<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>Go语言学习 | Depicter</title><meta name="author" content="fanhao"><meta name="copyright" content="fanhao"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="ffffff"><meta name="description" content="Go语言学习1 快速上手1.1 包管理关于包管理的总结:  一个文件夹可以称为一个包 在文件夹(包)中可以创建多个文件 在同一个包下的每个文件中必须指定包, 且名字相同  重点: 关于包的分类  main包 main包下必须写一个main函数, 入口函数(主函数), 编译生成一个可执行文件   非main包 用来将代码分类, 分别放在不同的包和文件夹中    1.2 输出在终端将想要展示的数据显示">
<meta property="og:type" content="article">
<meta property="og:title" content="Go语言学习">
<meta property="og:url" content="https://crappier.github.io/2023/05/16/Go%E8%AF%AD%E8%A8%80%E5%AD%A6%E4%B9%A0/index.html">
<meta property="og:site_name" content="Depicter">
<meta property="og:description" content="Go语言学习1 快速上手1.1 包管理关于包管理的总结:  一个文件夹可以称为一个包 在文件夹(包)中可以创建多个文件 在同一个包下的每个文件中必须指定包, 且名字相同  重点: 关于包的分类  main包 main包下必须写一个main函数, 入口函数(主函数), 编译生成一个可执行文件   非main包 用来将代码分类, 分别放在不同的包和文件夹中    1.2 输出在终端将想要展示的数据显示">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://crappier.github.io/img/cover/cover_4.jpg">
<meta property="article:published_time" content="2023-05-16T12:02:35.000Z">
<meta property="article:modified_time" content="2024-03-13T08:20:28.007Z">
<meta property="article:author" content="fanhao">
<meta property="article:tag" content="Go语言">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://crappier.github.io/img/cover/cover_4.jpg"><link rel="shortcut icon" href="/img/dinosaur.png"><link rel="canonical" href="https://crappier.github.io/2023/05/16/Go%E8%AF%AD%E8%A8%80%E5%AD%A6%E4%B9%A0/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":false,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: true,
    post: true
  },
  runtime: '天',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  }
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'Go语言学习',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2024-03-13 16:20:28'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
    win.getCSS = (url,id = false) => new Promise((resolve, reject) => {
      const link = document.createElement('link')
      link.rel = 'stylesheet'
      link.href = url
      if (id) link.id = id
      link.onerror = reject
      link.onload = link.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        link.onload = link.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(link)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', 'ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.3.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/avatar.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">10</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">5</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">0</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于笔者</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg fixed" id="page-header" style="background-image: url('/img/cover/cover_4.jpg')"><nav id="nav"><span id="blog-info"><a href="/" title="Depicter"><span class="site-name">Depicter</span></a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于笔者</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Go语言学习</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2023-05-16T12:02:35.000Z" title="发表于 2023-05-16 20:02:35">2023-05-16</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2024-03-13T08:20:28.007Z" title="更新于 2024-03-13 16:20:28">2024-03-13</time></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="Go语言学习"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="Go语言学习"><a href="#Go语言学习" class="headerlink" title="Go语言学习"></a>Go语言学习</h1><h2 id="1-快速上手"><a href="#1-快速上手" class="headerlink" title="1 快速上手"></a>1 快速上手</h2><h3 id="1-1-包管理"><a href="#1-1-包管理" class="headerlink" title="1.1 包管理"></a>1.1 包管理</h3><p>关于包管理的总结:</p>
<ul>
<li>一个文件夹可以称为一个包</li>
<li>在文件夹(包)中可以创建多个文件</li>
<li>在同一个包下的每个文件中必须指定包, 且名字相同</li>
</ul>
<p>重点: 关于包的分类</p>
<ul>
<li>main包<ul>
<li>main包下必须写一个main函数, 入口函数(主函数), 编译生成一个可执行文件</li>
</ul>
</li>
<li>非main包<ul>
<li>用来将代码分类, 分别放在不同的包和文件夹中</li>
</ul>
</li>
</ul>
<h3 id="1-2-输出"><a href="#1-2-输出" class="headerlink" title="1.2 输出"></a>1.2 输出</h3><p>在终端将想要展示的数据显示出来</p>
<ul>
<li>内置函数<ul>
<li>print</li>
<li>println</li>
</ul>
</li>
</ul>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">    <span class="comment">// 基于内置函数(不推荐) </span></span><br><span class="line">    <span class="comment">// print无换行</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;Hello world&quot;</span>)</span><br><span class="line">    <span class="built_in">println</span>(<span class="string">&quot;Hello Golang&quot;</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>fmt包(推荐)<ul>
<li>fmt.print</li>
<li>fmt.println</li>
</ul>
</li>
</ul>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> <span class="string">&quot;fmt&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">    <span class="comment">// fmt包(推荐) </span></span><br><span class="line">    fmt.Print(<span class="string">&quot;Hello Python&quot;</span>)</span><br><span class="line">    fmt.Println(<span class="string">&quot;Hello Golang&quot;</span>)</span><br><span class="line">    fmt.Printf(<span class="string">&quot;我是%s\n&quot;</span>, <span class="string">&quot;李白&quot;</span>)</span><br><span class="line">    fmt.Printf(<span class="string">&quot;我今天喝了%d瓶酒&quot;</span>, <span class="number">34</span>)</span><br><span class="line">    fmt.Printf(<span class="string">&quot;欠了%.3f元&quot;</span>, <span class="number">87.321</span>)</span><br><span class="line">    fmt.Printf(<span class="string">&quot;现在还了%d%%&quot;</span>, <span class="number">13</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>扩展: 进程中有stdin, stdout, stderr</p>
<h3 id="1-3-注释"><a href="#1-3-注释" class="headerlink" title="1.3 注释"></a>1.3 注释</h3><ul>
<li>单行注释</li>
</ul>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 单行注释</span></span><br></pre></td></tr></table></figure>

<ul>
<li>多行注释</li>
</ul>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">多</span></span><br><span class="line"><span class="comment">行</span></span><br><span class="line"><span class="comment">注</span></span><br><span class="line"><span class="comment">释</span></span><br><span class="line"><span class="comment"> */</span></span><br></pre></td></tr></table></figure>

<h3 id="1-4-数据类型-初始"><a href="#1-4-数据类型-初始" class="headerlink" title="1.4 数据类型(初始)"></a>1.4 数据类型(初始)</h3><ul>
<li>整型, 数字</li>
<li>字符串, 文本</li>
<li>布尔型, 真假</li>
</ul>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> <span class="string">&quot;fmt&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">  <span class="comment">// 整型 </span></span><br><span class="line">  fmt.Println(<span class="number">100</span>)</span><br><span class="line">  fmt.Println(<span class="number">100</span> - <span class="number">89</span>)</span><br><span class="line">  fmt.Println(<span class="number">18</span> % <span class="number">7</span>)</span><br><span class="line">  <span class="comment">// 字符串</span></span><br><span class="line">  fmt.Println(<span class="string">&quot;1&quot;</span>+<span class="string">&quot;2&quot;</span>)</span><br><span class="line">  fmt.Println(<span class="string">&quot;DJGN&quot;</span>)</span><br><span class="line">  <span class="comment">// 布尔类型</span></span><br><span class="line">  fmt.Println(<span class="number">1</span> &gt; <span class="number">2</span>) <span class="comment">// false</span></span><br><span class="line">  fmt.Println(<span class="number">1</span> &lt; <span class="number">2</span>) <span class="comment">// true</span></span><br><span class="line">  <span class="keyword">if</span> <span class="number">2</span> &gt; <span class="number">1</span> &#123;</span><br><span class="line">    fmt.Println(<span class="number">2</span>)</span><br><span class="line">  &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    fmt.Println(<span class="number">1</span>)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="1-5-变量"><a href="#1-5-变量" class="headerlink" title="1.5 变量"></a>1.5 变量</h3><p>可理解为昵称</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">    <span class="comment">// 定义变量并且赋值</span></span><br><span class="line">    <span class="keyword">var</span> num <span class="type">int</span> = <span class="number">10</span></span><br><span class="line">    <span class="keyword">var</span> name <span class="type">string</span> = <span class="string">&quot;libai&quot;</span></span><br><span class="line">    <span class="keyword">var</span> flag <span class="type">bool</span> = <span class="literal">true</span></span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 声明变量</span></span><br><span class="line">    <span class="keyword">var</span> num2 <span class="type">int</span></span><br><span class="line">    <span class="comment">// 给num2变量赋值 </span></span><br><span class="line">    num2 = <span class="number">10</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>声明变量的意义:</p>
<ul>
<li>编写代码时省事</li>
<li>存储结果, 方便之后使用</li>
<li>储存用户输入的值</li>
</ul>
<h4 id="1-5-1-变量名的要求"><a href="#1-5-1-变量名的要求" class="headerlink" title="1.5.1 变量名的要求"></a>1.5.1 变量名的要求</h4><ul>
<li>变量名只能包含: 字母, 数字, 下划线</li>
<li>数字不能开头</li>
<li>不能使用Go内置的关键字</li>
</ul>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">break</span>, <span class="keyword">default</span>, <span class="function"><span class="keyword">func</span>, <span class="title">interface</span>, <span class="title">select</span>, <span class="title">case</span>, <span class="title">defer</span>, <span class="title">go</span>,</span> </span><br><span class="line"><span class="keyword">map</span>, <span class="keyword">struct</span>, <span class="keyword">chan</span>, <span class="keyword">else</span>, <span class="keyword">goto</span>, <span class="keyword">package</span>, <span class="keyword">switch</span>, <span class="keyword">const</span>, <span class="keyword">fallthrough</span>, </span><br><span class="line"><span class="keyword">if</span>, <span class="keyword">range</span>, <span class="keyword">type</span>, <span class="keyword">continue</span>, <span class="keyword">for</span>, <span class="keyword">import</span>, <span class="keyword">return</span>, <span class="keyword">var</span></span><br></pre></td></tr></table></figure>

<ul>
<li>变量名见名知意: name&#x2F;age&#x2F;num</li>
<li>驼峰式命名: myBossName, startDate</li>
</ul>
<h4 id="1-5-2-变量简写"><a href="#1-5-2-变量简写" class="headerlink" title="1.5.2 变量简写"></a>1.5.2 变量简写</h4><ul>
<li>声明+赋值</li>
</ul>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> name <span class="type">string</span> = <span class="string">&quot;mnk&quot;</span></span><br><span class="line"><span class="keyword">var</span> name = <span class="string">&quot;mnk&quot;</span></span><br><span class="line">name := <span class="string">&quot;mnk&quot;</span> <span class="comment">// 推荐</span></span><br></pre></td></tr></table></figure>

<ul>
<li>先声明, 再赋值</li>
</ul>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> name <span class="type">string</span></span><br><span class="line"><span class="keyword">var</span> mess <span class="type">string</span></span><br><span class="line"><span class="keyword">var</span> data <span class="type">string</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> name, mess, data <span class="type">string</span></span><br><span class="line">name = <span class="string">&quot;mnk&quot;</span></span><br><span class="line">mess = <span class="string">&quot;none&quot;</span></span><br><span class="line">data = <span class="string">&quot;json&quot;</span></span><br></pre></td></tr></table></figure>

<p>因式分解概念, 例如:<br>声明5个变量, 分别1有字符串, 正兴</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> (</span><br><span class="line">  name = <span class="string">&quot;mnk&quot;</span></span><br><span class="line">  age = <span class="number">18</span></span><br><span class="line">  data = <span class="string">&quot;none&quot;</span></span><br><span class="line">  gender <span class="type">string</span> <span class="comment">// 只声明不赋值, 会有一个默认值: &quot;&quot;</span></span><br><span class="line">  length <span class="type">int</span> <span class="comment">// 默认值: 0</span></span><br><span class="line">  sb <span class="type">bool</span> <span class="comment">// 默认值: false</span></span><br><span class="line">)</span><br></pre></td></tr></table></figure>

<p>拓展: 在Go编译器中, 声明变量不使用则会报错</p>
<h4 id="1-5-3-变量作用域"><a href="#1-5-3-变量作用域" class="headerlink" title="1.5.3 变量作用域"></a>1.5.3 变量作用域</h4><p>如果定义了大括号, 则在大括号中定义的变量</p>
<ul>
<li>不能被上级调用</li>
<li>可以在同级调用</li>
<li>可以在子级调用</li>
</ul>
<ol>
<li>全局变量<br> 在函数外部定义的变量</li>
</ol>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 全局变量定义不能使用省略 :=</span></span><br><span class="line"><span class="keyword">var</span> name1 <span class="type">string</span> = <span class="string">&quot;nmk&quot;</span></span><br><span class="line"><span class="keyword">var</span> name2 = <span class="string">&quot;nmk2&quot;</span></span><br><span class="line"><span class="comment">// 可以基于因式分解定义多个变量</span></span><br><span class="line"><span class="keyword">var</span> (</span><br><span class="line">    v1 = <span class="number">10</span></span><br><span class="line">    v2 = <span class="string">&quot;v2&quot;</span></span><br><span class="line">    v3 = <span class="literal">true</span></span><br><span class="line">)</span><br></pre></td></tr></table></figure>

<ol start="2">
<li>局部变量<br> 编写在函数内(大括号内)的变量, 可以使用任意方式简化, 可以基于因式分解方式声明多个变量</li>
</ol>
<h4 id="1-5-4-赋值及内存相关"><a href="#1-5-4-赋值及内存相关" class="headerlink" title="1.5.4 赋值及内存相关"></a>1.5.4 赋值及内存相关</h4><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">name := <span class="string">&#x27;libai&#x27;</span></span><br><span class="line">nick_name := name</span><br></pre></td></tr></table></figure>

<p><code>name</code> 指向’libai’的内存地址<br><code>nick_name</code> 指向’libai’(拷贝一份)的内存地址<br>注意: 与Python的等语言不同<br>eg:</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> <span class="string">&quot;fmt&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">  name := <span class="string">&quot;libai&quot;</span></span><br><span class="line">  nick_name := name</span><br><span class="line">  </span><br><span class="line">  fmt.Println(name, &amp;name)</span><br><span class="line">  <span class="comment">// libai 0xc000060270</span></span><br><span class="line">  fmt.Println(nick_name, &amp;nick_name)</span><br><span class="line">  <span class="comment">// libai 0xc000060280</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>如果变量指向发生了改变, 更改后的内容仍占用原内存地址</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> <span class="string">&quot;fmt&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">  name := <span class="string">&quot;libai&quot;</span></span><br><span class="line">  fmt.Println(name, &amp;name)</span><br><span class="line">  <span class="comment">// libai 0xc000060270</span></span><br><span class="line">  name = <span class="string">&quot;wangwei&quot;</span></span><br><span class="line">  fmt.Println(name, &amp;name)</span><br><span class="line">  <span class="comment">// wangwei 0xc000060270</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>注意:</p>
<ul>
<li>使用int&#x2F;string&#x2F;bool时, 如果遇到变量的赋值, 则会再拷贝一份 [值类型]</li>
</ul>
<h3 id="1-6-常量"><a href="#1-6-常量" class="headerlink" title="1.6 常量"></a>1.6 常量</h3><p>不可以被修改的变量, 一般情况下常量都定义为全局</p>
<h4 id="定义"><a href="#定义" class="headerlink" title="定义"></a>定义</h4><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> n1 <span class="type">int</span> = <span class="number">123</span></span><br><span class="line"><span class="keyword">const</span> n2 = <span class="number">456</span></span><br><span class="line"><span class="keyword">const</span> (</span><br><span class="line">  name = <span class="string">&quot;libai&quot;</span></span><br><span class="line">  age = <span class="number">98</span></span><br><span class="line">)</span><br></pre></td></tr></table></figure>

<h4 id="iota"><a href="#iota" class="headerlink" title="iota"></a>iota</h4><p>可有可无, 可以当做一个在声明常量时的计数器</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> (</span><br><span class="line">    v1 = <span class="literal">iota</span></span><br><span class="line">    v2</span><br><span class="line">    v3</span><br><span class="line">    v4</span><br><span class="line">    v5</span><br><span class="line">)</span><br><span class="line">fmt.Println(v1, v2, v3, v4, v5)</span><br><span class="line"><span class="comment">// 0 1 2 3 4</span></span><br><span class="line"><span class="keyword">const</span> (</span><br><span class="line">  v1 = <span class="literal">iota</span> + <span class="number">1</span></span><br><span class="line">  v2</span><br><span class="line">  v3</span><br><span class="line">  v4</span><br><span class="line">  v5</span><br><span class="line">)</span><br><span class="line">fmt.Println(v1, v2, v3, v4, v5)</span><br><span class="line"><span class="comment">// 1 2 3 4 5</span></span><br></pre></td></tr></table></figure>

<h3 id="1-7-输入"><a href="#1-7-输入" class="headerlink" title="1.7 输入"></a>1.7 输入</h3><p>让用户输入数据完成项目的交互操作</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">fmt.Scan</span><br><span class="line"><span class="comment">// fmt.Scan</span></span><br><span class="line"><span class="keyword">var</span> na <span class="type">string</span></span><br><span class="line"><span class="keyword">var</span> ag <span class="type">int</span></span><br><span class="line">fmt.Println(<span class="string">&quot;请输入用户名&quot;</span>)</span><br><span class="line"><span class="comment">// 当使用Scan时, 会提示用户输入</span></span><br><span class="line"><span class="comment">// 用户输入完成之后, 会有两个返回值: count, 用户输入了几个值; err, 用户输入错误则是错误信息</span></span><br><span class="line">_, err := fmt.Scan(&amp;na, &amp;ag)</span><br><span class="line"><span class="keyword">if</span> err == <span class="literal">nil</span> &#123;</span><br><span class="line">fmt.Println(na, ag)</span><br><span class="line">&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">fmt.Println(<span class="string">&quot;用户输入错误&quot;</span>, err)</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 说明: fmt.Scan 如果要求输入两个值, 则必须输入两个, 否则会一直等待...</span></span><br><span class="line"></span><br><span class="line">fmt.Scanln - 用的最多</span><br><span class="line"><span class="keyword">var</span> na2 <span class="type">string</span></span><br><span class="line"><span class="keyword">var</span> ag2 <span class="type">int</span></span><br><span class="line">fmt.Print(<span class="string">&quot;请输入用户名: &quot;</span>)</span><br><span class="line">cnt, err := fmt.Scanln(&amp;na2, &amp;ag2)</span><br><span class="line">fmt.Println(na2, ag2)</span><br><span class="line">fmt.Println(cnt, err)</span><br><span class="line"><span class="comment">// 说明: fmt.Scanln 等待回车, 如果按了回车表示输入完成</span></span><br><span class="line"></span><br><span class="line">fmt.Scanf</span><br><span class="line"><span class="keyword">var</span> name <span class="type">string</span></span><br><span class="line"><span class="keyword">var</span> age <span class="type">int</span></span><br><span class="line">fmt.Println(<span class="string">&quot;请输入用户名&quot;</span>)</span><br><span class="line">fmt.Scanf(<span class="string">&quot;我是%s 今年%d 岁&quot;</span>, &amp;name, &amp;age)</span><br><span class="line">fmt.Println(name, age)</span><br></pre></td></tr></table></figure>

<p>无法解决的难题: 当输入信有空格时, 无法获取含空格的信息<br>解决方法:</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 使用标准输入</span></span><br><span class="line">reader := bufio.NewReader(os.Stdin)</span><br><span class="line"><span class="comment">// line, 从stdin中读取的一行数据(字节集合 -&gt; 转换成字符串)</span></span><br><span class="line"><span class="comment">// reader默认一次可以度4096个字节 (4096/3个汉字)</span></span><br><span class="line"><span class="comment">// 	- 一次性读完, isPrefix = false</span></span><br><span class="line"><span class="comment">//  - 先读一部分, isPrefix = true, 再去读取 isPrefix = false</span></span><br><span class="line">line, isPrefix, err := reader.ReadLine()</span><br><span class="line">data := <span class="type">string</span>(line)</span><br><span class="line">fmt.Println(data)</span><br><span class="line">fmt.Println(isPrefix, err)</span><br></pre></td></tr></table></figure>

<h3 id="1-8-条件语句"><a href="#1-8-条件语句" class="headerlink" title="1.8 条件语句"></a>1.8 条件语句</h3><p>条件的嵌套不要太多</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> condition &#123;</span><br><span class="line">    s1...</span><br><span class="line">&#125; <span class="keyword">else</span> <span class="keyword">if</span> condition2&#123;</span><br><span class="line">    s2...</span><br><span class="line">    <span class="keyword">if</span> condition2_1 &#123;</span><br><span class="line">        s2_1...</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        s2_2...	</span><br><span class="line">    &#125;</span><br><span class="line">&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    s3...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li><code>&amp;&amp;</code> 与</li>
<li><code>||</code> 或</li>
</ul>
<h2 id="2-基础知识"><a href="#2-基础知识" class="headerlink" title="2 基础知识"></a>2 基础知识</h2><h3 id="2-1-switch语句"><a href="#2-1-switch语句" class="headerlink" title="2.1 switch语句"></a>2.1 switch语句</h3><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// switch语句</span></span><br><span class="line"><span class="comment">// 1. 表达式</span></span><br><span class="line"><span class="keyword">switch</span> <span class="number">1</span> + <span class="number">1</span> &#123;</span><br><span class="line"><span class="keyword">case</span> <span class="number">1</span>:</span><br><span class="line">    fmt.Println(<span class="number">1</span>)</span><br><span class="line"><span class="keyword">case</span> <span class="number">2</span>:</span><br><span class="line">    fmt.Println(<span class="number">2</span>)</span><br><span class="line"><span class="keyword">case</span> <span class="number">3</span>:</span><br><span class="line">    fmt.Println(<span class="number">3</span>)</span><br><span class="line"><span class="keyword">default</span>:</span><br><span class="line">    fmt.Println(<span class="string">&quot;都不满足&quot;</span>)</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 2. 变量</span></span><br><span class="line">num := <span class="number">3</span></span><br><span class="line"><span class="keyword">switch</span> num &#123;</span><br><span class="line"><span class="keyword">case</span> <span class="number">1</span>:</span><br><span class="line">    fmt.Println(<span class="number">1</span>)</span><br><span class="line"><span class="keyword">case</span> <span class="number">2</span>:</span><br><span class="line">    fmt.Println(<span class="number">2</span>)</span><br><span class="line"><span class="keyword">case</span> <span class="number">3</span>:</span><br><span class="line">    fmt.Println(<span class="number">3</span>)</span><br><span class="line"><span class="keyword">default</span>:</span><br><span class="line">    fmt.Println(<span class="string">&quot;都不满足&quot;</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>注意事项: 数据类型一致的比较</p>
<h3 id="2-2-for循环"><a href="#2-2-for循环" class="headerlink" title="2.2 for循环"></a>2.2 for循环</h3><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 1. 死循环</span></span><br><span class="line"><span class="keyword">for</span> &#123;</span><br><span class="line">    fmt.Println(<span class="string">&quot;hello&quot;</span>)</span><br><span class="line">    time.Sleep(time.Second * <span class="number">1</span>)</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 2. 条件循环, 如果满足条件, 则执行, 每次循环都会再次判断是否满足条件</span></span><br><span class="line"><span class="keyword">for</span> <span class="number">2</span> &gt; <span class="number">1</span> &#123;</span><br><span class="line">    fmt.Println(<span class="string">&quot;hello&quot;</span>)</span><br><span class="line">    time.Sleep(time.Second * <span class="number">1</span>)</span><br><span class="line">&#125;</span><br><span class="line">number := <span class="number">10</span></span><br><span class="line"><span class="keyword">for</span> number &gt; <span class="number">0</span> &#123;</span><br><span class="line">    fmt.Println(number)</span><br><span class="line">    number -= <span class="number">1</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">for</span> number &gt; <span class="number">0</span> &#123;</span><br><span class="line">    number -= <span class="number">1</span></span><br><span class="line">    <span class="keyword">if</span> number == <span class="number">5</span> &#123;</span><br><span class="line">        <span class="keyword">break</span></span><br><span class="line">    &#125;</span><br><span class="line">    fmt.Println(number)</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">for</span> number &gt; <span class="number">0</span> &#123;</span><br><span class="line">    number -= <span class="number">1</span></span><br><span class="line">    <span class="keyword">if</span> number == <span class="number">5</span> &#123;</span><br><span class="line">        <span class="keyword">continue</span></span><br><span class="line">    &#125;</span><br><span class="line">    fmt.Println(number)</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 3. </span></span><br><span class="line"><span class="keyword">for</span> i := <span class="number">1</span>; i &lt; <span class="number">10</span>; i++ &#123;</span><br><span class="line">    fmt.Println(i)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>当循环遇到continue时, 会停止当前循环, 开始下一次循环<br>当循环遇到break时, 会直接跳出循环</p>
<h3 id="2-3-for循环打标签"><a href="#2-3-for循环打标签" class="headerlink" title="2.3 for循环打标签"></a>2.3 for循环打标签</h3><p>对for循环打标签, 可以通过continue和break实现多层循环的跳出和终止</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">f1:</span><br><span class="line">    <span class="keyword">for</span> i := <span class="number">1</span>; i &lt; <span class="number">3</span>; i++ &#123;</span><br><span class="line">        <span class="keyword">for</span> j := <span class="number">1</span>; j &lt; <span class="number">5</span>; j++ &#123;</span><br><span class="line">            <span class="keyword">if</span> j == <span class="number">3</span> &#123;</span><br><span class="line">                <span class="keyword">continue</span> f1</span><br><span class="line">            &#125;</span><br><span class="line">            fmt.Println(i, j)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"><span class="comment">// 1 1</span></span><br><span class="line"><span class="comment">// 1 2</span></span><br><span class="line"><span class="comment">// 2 1</span></span><br><span class="line"><span class="comment">// 2 2</span></span><br></pre></td></tr></table></figure>

<h3 id="2-4-goto语句"><a href="#2-4-goto语句" class="headerlink" title="2.4 goto语句"></a>2.4 goto语句</h3><p>跳跃到指定的行, 然后向下执行代码</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">    <span class="keyword">var</span> name <span class="type">string</span></span><br><span class="line">    fmt.Println(<span class="string">&quot;请输入姓名: &quot;</span>)</span><br><span class="line">    _, _ = fmt.Scanln(&amp;name)</span><br><span class="line">    <span class="keyword">if</span> name == <span class="string">&quot;libai&quot;</span> &#123;</span><br><span class="line">        <span class="comment">// vip</span></span><br><span class="line">        <span class="keyword">goto</span> VIP</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="comment">// 普通人</span></span><br><span class="line">        <span class="keyword">goto</span> COM</span><br><span class="line">    &#125;</span><br><span class="line">COM:</span><br><span class="line">    fmt.Println(<span class="string">&quot;付款...&quot;</span>)</span><br><span class="line">VIP:</span><br><span class="line">    fmt.Println(<span class="string">&quot;喝酒&quot;</span>)</span><br></pre></td></tr></table></figure>

<h3 id="2-5-字符串格式化"><a href="#2-5-字符串格式化" class="headerlink" title="2.5 字符串格式化"></a>2.5 字符串格式化</h3><p>将数据格式化为特定格式的字符串时, 可以使用字符串格式化</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 字符串格式化</span></span><br><span class="line"><span class="keyword">var</span> name, address, act <span class="type">string</span></span><br><span class="line">fmt.Println(<span class="string">&quot;请输入姓名, 地址, 行为&quot;</span>)</span><br><span class="line">_, _ = fmt.Scanln(&amp;name, &amp;address, &amp;act)</span><br><span class="line">res := fmt.Sprintf(<span class="string">&quot;我是%s, 我在%s, 我在%s&quot;</span>, name, address, act)</span><br><span class="line">fmt.Println(res)</span><br><span class="line"><span class="comment">// 我是李白, 我在皇宫, 我在喝酒</span></span><br></pre></td></tr></table></figure>

<h3 id="2-6-运算符"><a href="#2-6-运算符" class="headerlink" title="2.6 运算符"></a>2.6 运算符</h3><figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line">算数运算符</span><br><span class="line">+ - * / % ++ --</span><br><span class="line">关系运算符</span><br><span class="line">== != &gt; &lt; &gt;= &lt;=</span><br><span class="line">逻辑运算符</span><br><span class="line">&amp;&amp; || !</span><br><span class="line">位运算符</span><br><span class="line">1. &amp; 按位与(全为1, 才为1)</span><br><span class="line">eg:</span><br><span class="line">    5  -&gt; 0000101</span><br><span class="line">    99 -&gt; 1100011</span><br><span class="line">5 &amp; 99 -&gt; 0000001 -&gt; 1</span><br><span class="line"></span><br><span class="line">2. | 按位或(只要有1, 就是1)</span><br><span class="line">eg: 5  -&gt; 0000101</span><br><span class="line">    99 -&gt; 1100011</span><br><span class="line">5 | 99 -&gt; 1100111 -&gt; 103</span><br><span class="line"></span><br><span class="line">3. ^ 按位异或(只要上下不同, 就是1)</span><br><span class="line">eg: 5  -&gt; 0000101</span><br><span class="line">    99 -&gt; 1100011</span><br><span class="line">5 ^ 99 -&gt; 1100110 -&gt; 102</span><br><span class="line"></span><br><span class="line">4. &lt;&lt; 按位向左移动</span><br><span class="line">eg: 5  -&gt; 101</span><br><span class="line">5 &lt;&lt; 2 -&gt; 10100 -&gt; 16+4=20</span><br><span class="line"></span><br><span class="line">5. &gt;&gt; 按位向右移动</span><br><span class="line">eg: 5  -&gt; 101</span><br><span class="line">5 &gt;&gt; 1 -&gt; 10 -&gt; 2</span><br><span class="line"></span><br><span class="line">6. &amp;^ 比较清除, 以前面的值为基准, 让前面的值和后面的值得二进制位进行比较, 如果两个位置都是1, 则将前面的值那个位置置为0</span><br><span class="line">eg: 5  -&gt; 0000101</span><br><span class="line">    99 -&gt; 1100011</span><br><span class="line">5 &amp;^99 -&gt; 0000100 -&gt; 4</span><br><span class="line"></span><br><span class="line">赋值运算符</span><br><span class="line">= += -= *= /= %=</span><br><span class="line">&lt;&lt;= &gt;&gt;= &amp;= ^= |=</span><br></pre></td></tr></table></figure>

<p>运算符的优先级</p>
<figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Precedence  Operator</span><br><span class="line">    5        * / % &lt;&lt; &gt;&gt; &amp; &amp;^</span><br><span class="line">    4        + - | ^</span><br><span class="line">    3        == != &lt; &lt;= &gt; &gt;=</span><br><span class="line">    2        &amp;&amp;</span><br><span class="line">    1        ||</span><br></pre></td></tr></table></figure>

<h2 id="3-数据类型"><a href="#3-数据类型" class="headerlink" title="3 数据类型"></a>3 数据类型</h2><ul>
<li>整型, 表示整数</li>
<li>浮点型, 表示小数</li>
<li>布尔型, 表示真假</li>
<li>数组, 表示数据集合</li>
<li>指针, 表示内存地址</li>
<li>切片, 表示数据集合的部分</li>
<li>字典, 表示键值对集合</li>
<li>结构体, 用于自定义数据集合</li>
<li>接口, 用于约束和泛指数据类型</li>
</ul>
<h3 id="3-1-整型"><a href="#3-1-整型" class="headerlink" title="3.1 整型"></a>3.1 整型</h3><p>Go中的整型分为有符号和无符号两大类, 有符号的包含负值, 无符号的不包含负值</p>
<h4 id="3-1-1-类型"><a href="#3-1-1-类型" class="headerlink" title="3.1.1 类型"></a>3.1.1 类型</h4><p>有符号整型:</p>
<ul>
<li><code>int8</code>  -2^7^~2^7^-1</li>
<li><code>int16</code> -2^15^~2^15^-1</li>
<li><code>int32</code> -2^31^~2^31^-1</li>
<li><code>int64</code> -2^63^~2^63^-1</li>
<li><code>int</code>, 在32位操作系统上使用32位&#x3D;int32, 在64位操作系统上使用64位&#x3D;int64</li>
</ul>
<p>无符号整型:</p>
<ul>
<li><code>uint8</code>  0~2^8^-1</li>
<li><code>uint16</code> 0~2^16^-1</li>
<li><code>uint32</code> 0~2^32^-1</li>
<li><code>uint64</code> 0~2^64^-1</li>
<li><code>uint</code>, 在32位操作系统上使用32位&#x3D;uint32, 在64位操作系统上使用64位&#x3D;uint64</li>
</ul>
<p>不同整型可表示的数据范围不同, 需要根据实际需求来选择合适的类型</p>
<h4 id="3-1-2-整型之间相互转换"><a href="#3-1-2-整型之间相互转换" class="headerlink" title="3.1.2 整型之间相互转换"></a>3.1.2 整型之间相互转换</h4><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> v1 <span class="type">int8</span> = <span class="number">10</span></span><br><span class="line"><span class="keyword">var</span> v2 <span class="type">int16</span> = <span class="number">18</span></span><br><span class="line">v3 := <span class="type">int16</span>(v1) + v2</span><br></pre></td></tr></table></figure>

<p>注意:</p>
<ul>
<li>低位转向高位时, 不会出问题</li>
<li>高位转向低位时, 可能会出现问题</li>
</ul>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> v1 <span class="type">int16</span> = <span class="number">128</span></span><br><span class="line">v2 := <span class="type">int8</span>(v1)</span><br><span class="line">fmt.Println(v2)</span><br><span class="line"><span class="comment">// -128</span></span><br></pre></td></tr></table></figure>

<h4 id="3-1-3-整型与字符串转换"><a href="#3-1-3-整型与字符串转换" class="headerlink" title="3.1.3 整型与字符串转换"></a>3.1.3 整型与字符串转换</h4><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">v1 := <span class="number">19</span></span><br><span class="line"><span class="comment">// 只能传入int类型</span></span><br><span class="line">res := strconv.Itoa(v1)</span><br><span class="line">fmt.Println(res, reflect.TypeOf(res))</span><br><span class="line"><span class="comment">// 19 string</span></span><br><span class="line"></span><br><span class="line">v2 := <span class="string">&quot;666&quot;</span></span><br><span class="line">res2, err := strconv.Atoi(v2)</span><br><span class="line">fmt.Println(res2, err)</span><br><span class="line"><span class="comment">// 转换后是int类型</span></span><br><span class="line"><span class="comment">// 666 &lt;nil&gt;</span></span><br><span class="line"></span><br><span class="line">v3 := <span class="string">&quot;sb&quot;</span></span><br><span class="line">res3, err := strconv.Atoi(v3)</span><br><span class="line">fmt.Println(res3, err)</span><br><span class="line"><span class="comment">// 0 strconv.Atoi: parsing &quot;sb&quot;: invalid syntax</span></span><br></pre></td></tr></table></figure>

<h4 id="3-1-4-进制转换"><a href="#3-1-4-进制转换" class="headerlink" title="3.1.4 进制转换"></a>3.1.4 进制转换</h4><ul>
<li>Go代码中:<ul>
<li>十进制, 整型的方式存在</li>
<li>其他进制, 字符串形式存在</li>
</ul>
</li>
</ul>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">v1 := <span class="number">99</span></span><br><span class="line"><span class="comment">// 整型转换为2进制</span></span><br><span class="line">r1 := strconv.FormatInt(<span class="type">int64</span>(v1), <span class="number">2</span>)</span><br><span class="line">fmt.Println(r1, reflect.TypeOf(r1))</span><br><span class="line"><span class="comment">// 1100011 string</span></span><br><span class="line"><span class="comment">// 整型转换为8进制</span></span><br><span class="line">r2 := strconv.FormatInt(<span class="type">int64</span>(v1), <span class="number">8</span>)</span><br><span class="line">fmt.Println(r2, reflect.TypeOf(r2))</span><br><span class="line"><span class="comment">// 143 string</span></span><br><span class="line"><span class="comment">// 整型转换为16进制</span></span><br><span class="line">r3 := strconv.FormatInt(<span class="type">int64</span>(v1), <span class="number">16</span>)</span><br><span class="line">fmt.Println(r3, reflect.TypeOf(r3))</span><br><span class="line"><span class="comment">// 63 string</span></span><br></pre></td></tr></table></figure>

<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 传入的data是待转换的文本</span></span><br><span class="line"><span class="comment">// 2是把文本当做2进制转换为10进制</span></span><br><span class="line"><span class="comment">// 16, 转换的过程中对结果进行约束 16-&gt;int16; 8-&gt;int8; 0-&gt;int</span></span><br><span class="line"><span class="comment">// 如果转换成功, 将err设置为nil, res永远是以int64的类型</span></span><br><span class="line">data := <span class="string">&quot;10010111001&quot;</span></span><br><span class="line">res, err := strconv.ParseInt(data, <span class="number">2</span>, <span class="number">16</span>)</span><br><span class="line">fmt.Println(res, err, reflect.TypeOf(res))</span><br><span class="line"><span class="comment">// 1209 &lt;nil&gt; int64</span></span><br></pre></td></tr></table></figure>

<p>通过ParseInt转换为10进制时, 类似于字符串转换为整数, 本质上与Atoi是相同的</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 练习:</span></span><br><span class="line"><span class="comment">// 1. 将17转换为16进制</span></span><br><span class="line">v1 := <span class="number">17</span></span><br><span class="line">res1 := strconv.FormatInt(<span class="type">int64</span>(v1), <span class="number">16</span>)</span><br><span class="line">fmt.Println(res1, reflect.TypeOf(res1))</span><br><span class="line"><span class="comment">// 2. 将2进制的10011转换为10进制的整数</span></span><br><span class="line">v2 := <span class="string">&quot;10011&quot;</span></span><br><span class="line">res2, err2 := strconv.ParseInt(v2, <span class="number">2</span>, <span class="number">0</span>)</span><br><span class="line">fmt.Println(res2, err2, reflect.TypeOf(res2))</span><br><span class="line"><span class="comment">// 2. 将2进制的10011转换为16进制的字符串</span></span><br><span class="line">res3 := strconv.FormatInt(res2, <span class="number">16</span>)</span><br><span class="line">fmt.Println(res3, reflect.TypeOf(res3))</span><br></pre></td></tr></table></figure>

<h4 id="3-1-5-常见数学运算"><a href="#3-1-5-常见数学运算" class="headerlink" title="3.1.5 常见数学运算"></a>3.1.5 常见数学运算</h4><blockquote>
<p>math包</p>
</blockquote>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 取绝对值</span></span><br><span class="line">math.Abs(<span class="number">-19</span>)</span><br><span class="line"><span class="comment">// 向下取整</span></span><br><span class="line">math.Floor(<span class="number">3.15</span>)</span><br><span class="line"><span class="comment">// 向上取整</span></span><br><span class="line">math.Ceil(<span class="number">3.4</span>)</span><br><span class="line"><span class="comment">// 四舍五入</span></span><br><span class="line">math.Round(<span class="number">3.345</span>)</span><br><span class="line"><span class="comment">// 保留小数点后2位</span></span><br><span class="line">math.Round(<span class="number">3.3456</span>*<span class="number">100</span>) / <span class="number">100</span></span><br><span class="line"><span class="comment">// 取余数</span></span><br><span class="line">math.Mod(<span class="number">11</span>, <span class="number">3</span>)</span><br><span class="line"><span class="comment">// 计算次方</span></span><br><span class="line">math.Pow(<span class="number">2</span>, <span class="number">5</span>) <span class="comment">// 2**5</span></span><br><span class="line"><span class="comment">// 计算10次方</span></span><br><span class="line">math.Pow10(<span class="number">2</span>)  <span class="comment">// 2**10</span></span><br><span class="line"><span class="comment">// 取两个数的较大值</span></span><br><span class="line">math.Max(<span class="number">1</span>, <span class="number">2</span>)</span><br><span class="line"><span class="comment">// 取两个数的较小值</span></span><br><span class="line">math.Min(<span class="number">3</span>, <span class="number">4</span>)</span><br><span class="line">....</span><br></pre></td></tr></table></figure>

<h4 id="3-1-6-指针-x2F-nil-x2F-声明变量-x2F-new"><a href="#3-1-6-指针-x2F-nil-x2F-声明变量-x2F-new" class="headerlink" title="3.1.6 指针&#x2F;nil&#x2F;声明变量&#x2F;new"></a>3.1.6 指针&#x2F;nil&#x2F;声明变量&#x2F;new</h4><ul>
<li>声明变量</li>
</ul>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 两种方式都会占用空间, v1默认值0, v2值999</span></span><br><span class="line"><span class="keyword">var</span> v1 <span class="type">int</span></span><br><span class="line">v2 := <span class="number">999</span></span><br></pre></td></tr></table></figure>

<ul>
<li>指针</li>
</ul>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// v3指向一块内存地址-&gt;0x0, 0x0存入nil</span></span><br><span class="line"><span class="keyword">var</span> v3 *<span class="type">int</span></span><br><span class="line"><span class="comment">// v4指向一块内存地址, v4的值为int(默认值=0)的地址()</span></span><br><span class="line">v4 := <span class="built_in">new</span>(<span class="type">int</span>)</span><br></pre></td></tr></table></figure>

<ul>
<li>new<br>  new用于创建内存, 并进行内部数据初始化, 并返回一个指针类型</li>
<li>nil<br>  nil指向Go中的空值</li>
</ul>
<h4 id="3-1-7-超大整型"><a href="#3-1-7-超大整型" class="headerlink" title="3.1.7 超大整型"></a>3.1.7 超大整型</h4><p>在创建超大整型对象时:</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 1. 创建一个超大整型的对象</span></span><br><span class="line"><span class="keyword">var</span> v1 big.Int</span><br><span class="line"><span class="comment">//var v2 *big.Int // 一般用不到, 直接赋值的时候使用</span></span><br><span class="line">v3 := <span class="built_in">new</span>(big.Int)</span><br><span class="line"><span class="comment">// 2. 在对象中写入值</span></span><br><span class="line">v1.SetInt64(<span class="number">1990</span>)</span><br><span class="line">fmt.Println(v1)</span><br><span class="line">v1.SetString(<span class="string">&quot;91823991241212412456564564564&quot;</span>, <span class="number">10</span>)</span><br><span class="line">fmt.Println(v1.String())</span><br><span class="line"></span><br><span class="line">v3.SetString(<span class="string">&quot;101010101010100110011&quot;</span>, <span class="number">2</span>)</span><br><span class="line">fmt.Println(v3.String())</span><br></pre></td></tr></table></figure>

<p>推荐使用指针的方式-&gt;使用new进行创建和初始化</p>
<p>超大整型的运算</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">n1 := <span class="built_in">new</span>(big.Int)</span><br><span class="line">n1.SetInt64(<span class="number">289</span>)</span><br><span class="line"><span class="comment">//n1 := big.NewInt(289)</span></span><br><span class="line">n2 := <span class="built_in">new</span>(big.Int)</span><br><span class="line">n2.SetInt64(<span class="number">32</span>)</span><br><span class="line"><span class="comment">//n2 := big.NewInt(32)</span></span><br><span class="line">res := <span class="built_in">new</span>(big.Int)</span><br><span class="line"><span class="comment">// 加</span></span><br><span class="line">res.Add(n1, n2)</span><br><span class="line">fmt.Println(res.String())</span><br><span class="line"><span class="comment">// 减</span></span><br><span class="line">res.Sub(n1, n2)</span><br><span class="line">fmt.Println(res.String())</span><br><span class="line"><span class="comment">// 乘</span></span><br><span class="line">res.Mul(n1, n2)</span><br><span class="line">fmt.Println(res.String())</span><br><span class="line"><span class="comment">// 除(整除)</span></span><br><span class="line">res.Div(n1, n2)</span><br><span class="line">fmt.Println(res.String())</span><br><span class="line"><span class="comment">// 除, 得余数和商</span></span><br><span class="line">minder := <span class="built_in">new</span>(big.Int)</span><br><span class="line">res.DivMod(n1, n2, minder)</span><br><span class="line">fmt.Println(res.String(), minder.String())</span><br></pre></td></tr></table></figure>

<ul>
<li>尽量使用new方法去初始化并返回一个指针类型的方式</li>
<li>int类型和*int类型不同</li>
</ul>
<h3 id="3-2-浮点型"><a href="#3-2-浮点型" class="headerlink" title="3.2 浮点型"></a>3.2 浮点型</h3><h4 id="3-2-1-类型"><a href="#3-2-1-类型" class="headerlink" title="3.2.1 类型"></a>3.2.1 类型</h4><p>Go中提供两种浮点型</p>
<ul>
<li>float32, 用32位(4字节)存储浮点型</li>
<li>float64, 用64位(8字节)存储浮点型</li>
</ul>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> v1 <span class="type">float32</span></span><br><span class="line">v1 = <span class="number">3.24</span></span><br><span class="line"><span class="comment">// v2默认是float64类型</span></span><br><span class="line">v2 := <span class="number">99.2</span></span><br><span class="line">fmt.Println(v1, v2, <span class="type">float64</span>(v1)+v2)</span><br></pre></td></tr></table></figure>

<h4 id="3-2-2-非精确"><a href="#3-2-2-非精确" class="headerlink" title="3.2.2 非精确"></a>3.2.2 非精确</h4><p>第一步: 将浮点型转换为二进制</p>
<ul>
<li>整数部分-&gt;直接转换为二进制</li>
<li>小数部分-&gt;让小数部分乘2, 如果结果小于1则将结果继续乘2, 结果大于1则-1, 再继续乘2, 结果等于1则结束</li>
</ul>
<p>所以, 最终将整数部分和小数部分拼接起来</p>
<p>第二步: 科学计数法表示</p>
<p>第三步: 储存<br>以float32为例-&gt;用32位来存储浮点数<br>32&#x3D;1+8+23<br>1 : sign - 符号<br>8 : exponent - 8位表示共有256种, 含正负值(-128~127)<br>23: fraction - 小数部分, 存储小数点后的所有的数据</p>
<p>float64与float32类似, 只是用于表示各部分的位数不同:<br>1 : sign<br>11: exponent<br>52: fraction</p>
<h4 id="3-2-3-deciaml"><a href="#3-2-3-deciaml" class="headerlink" title="3.2.3 deciaml"></a>3.2.3 deciaml</h4><p>Go语言内部没有deciaml<br>第三方包, 需要在Go环境中先安装再使用, 地址: <a target="_blank" rel="noopener" href="https://github.com/shopspring/decimal">https://github.com/shopspring/decimal</a></p>
<p>安装:<br><code>go get github.com/shopspring/decimal</code></p>
<p>使用deciaml包</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> (</span><br><span class="line">  <span class="string">&quot;fmt&quot;</span></span><br><span class="line">  <span class="string">&quot;github.com/shopspring/decimal&quot;</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> v1 = decimal.NewFromFloat(<span class="number">0.000019</span>)</span><br><span class="line"><span class="keyword">var</span> v2 = decimal.NewFromFloat(<span class="number">0.29</span>)</span><br><span class="line">v3 := v1.Add(v2)</span><br><span class="line">fmt.Println(v3)</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> price = decimal.NewFromFloat(<span class="number">3.45</span>)</span><br><span class="line"><span class="comment">// 四舍五入, 保留1位</span></span><br><span class="line"><span class="keyword">var</span> data1 = price.Round(<span class="number">1</span>)</span><br><span class="line"><span class="comment">// 保留1位, 直接舍去</span></span><br><span class="line"><span class="keyword">var</span> data2 = price.Truncate(<span class="number">1</span>)</span><br><span class="line">fmt.Println(data1, data2)</span><br></pre></td></tr></table></figure>

<h3 id="3-3-布尔类型"><a href="#3-3-布尔类型" class="headerlink" title="3.3 布尔类型"></a>3.3 布尔类型</h3><p>表示真假, 一般是和条件配合使用, 用于满足某个条件时, 执行某个操作</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 字符串转换为bool</span></span><br><span class="line">res, err := strconv.ParseBool(<span class="string">&quot;t&quot;</span>)</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    switch str &#123;</span></span><br><span class="line"><span class="comment">    case &quot;1&quot;, &quot;t&quot;, &quot;T&quot;, &quot;true&quot;, &quot;TRUE&quot;, &quot;True&quot;:</span></span><br><span class="line"><span class="comment">        return true, nil</span></span><br><span class="line"><span class="comment">    case &quot;0&quot;, &quot;f&quot;, &quot;F&quot;, &quot;false&quot;, &quot;FALSE&quot;, &quot;False&quot;:</span></span><br><span class="line"><span class="comment">        return false, nil</span></span><br><span class="line"><span class="comment">    &#125;</span></span><br><span class="line"><span class="comment">    return false, syntaxError(&quot;ParseBool&quot;, str)</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line">fmt.Println(res, err)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 布尔转字符串</span></span><br><span class="line">res2 := strconv.FormatBool(<span class="literal">true</span>)</span><br><span class="line">fmt.Println(res2, reflect.TypeOf(res2))</span><br></pre></td></tr></table></figure>

<h3 id="3-4-字符串类型"><a href="#3-4-字符串类型" class="headerlink" title="3.4 字符串类型"></a>3.4 字符串类型</h3><p>字符串用于对文本进行处理</p>
<h4 id="3-4-1-字符串的本质"><a href="#3-4-1-字符串的本质" class="headerlink" title="3.4.1 字符串的本质"></a>3.4.1 字符串的本质</h4><p>计算机中所有的操作和数据最终都是字符串, 如: 100010101<br>Go语言中的字符串是utf-8编码的序列</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">name := <span class="string">&quot;李白&quot;</span></span><br><span class="line"><span class="comment">// 李: 11100110 10011101 10001110</span></span><br><span class="line">fmt.Println(name[<span class="number">0</span>], strconv.FormatInt(<span class="type">int64</span>(name[<span class="number">0</span>]), <span class="number">2</span>))</span><br><span class="line">fmt.Println(name[<span class="number">1</span>], strconv.FormatInt(<span class="type">int64</span>(name[<span class="number">1</span>]), <span class="number">2</span>))</span><br><span class="line">fmt.Println(name[<span class="number">2</span>], strconv.FormatInt(<span class="type">int64</span>(name[<span class="number">2</span>]), <span class="number">2</span>))</span><br><span class="line"><span class="comment">// 白: 11100111 10011001 10111101</span></span><br><span class="line">fmt.Println(name[<span class="number">3</span>], strconv.FormatInt(<span class="type">int64</span>(name[<span class="number">3</span>]), <span class="number">2</span>))</span><br><span class="line">fmt.Println(name[<span class="number">4</span>], strconv.FormatInt(<span class="type">int64</span>(name[<span class="number">4</span>]), <span class="number">2</span>))</span><br><span class="line">fmt.Println(name[<span class="number">5</span>], strconv.FormatInt(<span class="type">int64</span>(name[<span class="number">5</span>]), <span class="number">2</span>))</span><br><span class="line"><span class="comment">// 2. 获取字符串的长度(字节长度)</span></span><br><span class="line">fmt.Println(<span class="built_in">len</span>(name)) <span class="comment">// 6</span></span><br><span class="line"><span class="comment">// 3. 字符串转换为&quot;字节集合&quot;</span></span><br><span class="line">byteSet := []<span class="type">byte</span>(name)</span><br><span class="line">fmt.Println(byteSet) <span class="comment">// [230 157 142 231 153 189]</span></span><br><span class="line"><span class="comment">// 4. 字节集合转换为字符串</span></span><br><span class="line">byteList := []<span class="type">byte</span>&#123;<span class="number">230</span>, <span class="number">157</span>, <span class="number">142</span>, <span class="number">231</span>, <span class="number">153</span>, <span class="number">189</span>&#125;</span><br><span class="line">tarString := <span class="type">string</span>(byteList)</span><br><span class="line">fmt.Println(tarString) <span class="comment">// 李白</span></span><br><span class="line"><span class="comment">// 5. 将字符串转换为 unicode字符集码点的集合</span></span><br><span class="line">tempSet := []<span class="type">rune</span>(name)</span><br><span class="line">fmt.Println(tempSet) <span class="comment">// [26446 30333]</span></span><br><span class="line"><span class="keyword">for</span> i := <span class="number">0</span>; i &lt; <span class="built_in">len</span>(tempSet); i++ &#123;</span><br><span class="line">  fmt.Println(tempSet[i], strconv.FormatInt(<span class="type">int64</span>(tempSet[i]), <span class="number">16</span>))</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 26446 674e -&gt; 李</span></span><br><span class="line"><span class="comment">// 30333 767d -&gt; 白</span></span><br><span class="line"><span class="comment">// 7. 长度的处理</span></span><br><span class="line"><span class="comment">// 获取字符长度</span></span><br><span class="line">fmt.Println(utf8.RuneCountInString(name)) <span class="comment">// 2</span></span><br></pre></td></tr></table></figure>

<h4 id="3-4-2-字符串的常见功能"><a href="#3-4-2-字符串的常见功能" class="headerlink" title="3.4.2 字符串的常见功能"></a>3.4.2 字符串的常见功能</h4><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br></pre></td><td class="code"><pre><span class="line">name := <span class="string">&quot;李白&quot;</span></span><br><span class="line"><span class="comment">// 1. 获取字节长度</span></span><br><span class="line">fmt.Println(<span class="built_in">len</span>(name))</span><br><span class="line"><span class="comment">// 2. 获取字符长度</span></span><br><span class="line">fmt.Println(utf8.RuneCountInString(name))</span><br><span class="line"><span class="comment">// 3. 是否已xx开头</span></span><br><span class="line">_ = strings.HasPrefix(name, <span class="string">&quot;李&quot;</span>)</span><br><span class="line"><span class="comment">// 4. 是否以xx结尾</span></span><br><span class="line">_ = strings.HasSuffix(name, <span class="string">&quot;白&quot;</span>)</span><br><span class="line"><span class="comment">// 5. 是否包含xx</span></span><br><span class="line">_ = strings.Contains(name, <span class="string">&quot;杜甫&quot;</span>) <span class="comment">// false</span></span><br><span class="line"><span class="comment">// 6. 转换为大写</span></span><br><span class="line">lowName := <span class="string">&quot;libai&quot;</span></span><br><span class="line">resLowToHigh := strings.ToUpper(lowName)</span><br><span class="line">fmt.Println(resLowToHigh)</span><br><span class="line"><span class="comment">// 7. 转换为小写</span></span><br><span class="line">highName := <span class="string">&quot;LIBAI&quot;</span></span><br><span class="line">resHighToLow := strings.ToLower(highName)</span><br><span class="line">fmt.Println(resHighToLow)</span><br><span class="line"><span class="comment">// 8. 去两边</span></span><br><span class="line">myStr := <span class="string">&quot;helloWorld&quot;</span></span><br><span class="line">res1 := strings.TrimLeft(myStr, <span class="string">&quot;he&quot;</span>)  <span class="comment">// 去除左边的he</span></span><br><span class="line">res2 := strings.TrimRight(myStr, <span class="string">&quot;ld&quot;</span>) <span class="comment">// 去除右边的ld</span></span><br><span class="line">res3 := strings.Trim(myStr, <span class="string">&quot;he&quot;</span>)      <span class="comment">// 去除两边的he</span></span><br><span class="line">fmt.Println(res1, res2, res3)</span><br><span class="line"><span class="comment">// 9. 替换</span></span><br><span class="line">myStr2 := <span class="string">&quot;hahahowyou&quot;</span></span><br><span class="line">res4 := strings.Replace(myStr2, <span class="string">&quot;ha&quot;</span>, <span class="string">&quot;Ha&quot;</span>, <span class="number">1</span>)  <span class="comment">// 找到ha, 替换为Ha, 从左到右找第一个替换</span></span><br><span class="line">res5 := strings.Replace(myStr2, <span class="string">&quot;ha&quot;</span>, <span class="string">&quot;Ha&quot;</span>, <span class="number">2</span>)  <span class="comment">// 找到ha, 替换为Ha, 从左到右找前两个替换</span></span><br><span class="line">res6 := strings.Replace(myStr2, <span class="string">&quot;ha&quot;</span>, <span class="string">&quot;Ha&quot;</span>, <span class="number">-1</span>) <span class="comment">// 找到ha, 替换为Ha, 全部替换</span></span><br><span class="line">fmt.Println(res4, res5, res6)</span><br><span class="line"><span class="comment">// 10. 分割</span></span><br><span class="line">score := <span class="string">&quot;100,87,99&quot;</span></span><br><span class="line">res7 := strings.Split(score, <span class="string">&quot;,&quot;</span>) <span class="comment">// 根据&quot;,&quot;进行字符串分割</span></span><br><span class="line">fmt.Println(res7)</span><br><span class="line"><span class="comment">// 11. 拼接</span></span><br><span class="line"><span class="keyword">var</span> builder strings.Builder</span><br><span class="line">builder.WriteString(<span class="string">&quot;Hello&quot;</span>)</span><br><span class="line">builder.WriteString(<span class="string">&quot;World&quot;</span>)</span><br><span class="line">val := builder.String()</span><br><span class="line">fmt.Println(val)</span><br><span class="line"><span class="comment">// 12. string转换为int</span></span><br><span class="line">num := <span class="string">&quot;678&quot;</span></span><br><span class="line"><span class="keyword">var</span> data, _ = strconv.Atoi(num)</span><br><span class="line">fmt.Println(data)</span><br><span class="line"><span class="comment">// 13. int 转换string</span></span><br><span class="line"><span class="keyword">var</span> res8 = strconv.Itoa(<span class="number">678</span>)</span><br><span class="line">fmt.Println(res8)</span><br><span class="line"><span class="comment">// 14. 字符串 与 字节切片</span></span><br><span class="line"><span class="comment">// a. -&gt;</span></span><br><span class="line">name = <span class="string">&quot;李白&quot;</span></span><br><span class="line">byteSet := []<span class="type">byte</span>(name)</span><br><span class="line">fmt.Println(byteSet)</span><br><span class="line"><span class="comment">// b. &lt;-</span></span><br><span class="line">byteList := []<span class="type">byte</span>&#123;<span class="number">230</span>, <span class="number">157</span>, <span class="number">142</span>, <span class="number">231</span>, <span class="number">153</span>, <span class="number">189</span>&#125;</span><br><span class="line">tarStr := <span class="type">string</span>(byteList)</span><br><span class="line">fmt.Println(tarStr)</span><br><span class="line"><span class="comment">// 15. 字符串 与 rune切片</span></span><br><span class="line"><span class="comment">// a. -&gt;</span></span><br><span class="line">name = <span class="string">&quot;李白&quot;</span></span><br><span class="line">tempSet := []<span class="type">rune</span>(name)</span><br><span class="line">fmt.Println(tempSet)</span><br><span class="line"><span class="comment">// b. &lt;-</span></span><br><span class="line">runeList := []<span class="type">rune</span>&#123;<span class="number">26446</span>, <span class="number">30333</span>&#125;</span><br><span class="line">tarName := <span class="type">string</span>(runeList)</span><br><span class="line">fmt.Println(tarName)</span><br><span class="line"><span class="comment">// 16. string 与 字符</span></span><br><span class="line"><span class="comment">// 数字转字符串</span></span><br><span class="line">v1 := <span class="type">string</span>(<span class="number">65</span>)</span><br><span class="line">fmt.Println(v1) <span class="comment">// A</span></span><br><span class="line">v2 := <span class="type">string</span>(<span class="number">26446</span>)</span><br><span class="line">fmt.Println(v2) <span class="comment">// 李</span></span><br><span class="line"><span class="comment">// 字符串转数字, size: 最短的字节</span></span><br><span class="line">v3, size := utf8.DecodeRuneInString(<span class="string">&quot;A&quot;</span>)</span><br><span class="line">fmt.Println(v3, size)</span><br></pre></td></tr></table></figure>

<h4 id="3-4-3-索引切片和循环"><a href="#3-4-3-索引切片和循环" class="headerlink" title="3.4.3 索引切片和循环"></a>3.4.3 索引切片和循环</h4><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 1. 索引获取字节</span></span><br><span class="line">name := <span class="string">&quot;李白&quot;</span></span><br><span class="line">v1 := name[<span class="number">0</span>]</span><br><span class="line">fmt.Println(v1)</span><br><span class="line"><span class="comment">// 2. 切片获取字节区间</span></span><br><span class="line">v2 := name[<span class="number">0</span>:<span class="number">3</span>]</span><br><span class="line">fmt.Println(v2)</span><br><span class="line"><span class="comment">// 3. 手动循环获取所有字节</span></span><br><span class="line"><span class="keyword">for</span> i := <span class="number">0</span>; i &lt; <span class="built_in">len</span>(name); i++ &#123;</span><br><span class="line">    fmt.Println(i, name[i])</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 4. for.. range..循环获取所有字符</span></span><br><span class="line"><span class="keyword">for</span> index, item := <span class="keyword">range</span> name &#123;</span><br><span class="line">    fmt.Println(index, item, <span class="type">string</span>(item))</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 5. 转换为rune集合</span></span><br><span class="line">dataList := []<span class="type">rune</span>(name)</span><br><span class="line">fmt.Println(dataList[<span class="number">0</span>], <span class="type">string</span>(dataList[<span class="number">0</span>]))</span><br></pre></td></tr></table></figure>

<h3 id="3-5-数组"><a href="#3-5-数组" class="headerlink" title="3.5 数组"></a>3.5 数组</h3><blockquote>
<p>定长且元素类型一致的数据集合</p>
</blockquote>
<h4 id="3-5-1-数组声明"><a href="#3-5-1-数组声明" class="headerlink" title="3.5.1 数组声明"></a>3.5.1 数组声明</h4><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 方式1: 先声明再赋值, 在内存中已经开辟了空间, 内存初始化的值为0</span></span><br><span class="line"><span class="keyword">var</span> numbers [<span class="number">3</span>]<span class="type">int</span></span><br><span class="line">numbers[<span class="number">0</span>] = <span class="number">123</span></span><br><span class="line">numbers[<span class="number">1</span>] = <span class="number">234</span></span><br><span class="line">numbers[<span class="number">2</span>] = <span class="number">345</span></span><br><span class="line"><span class="comment">// 方式2: 声明+赋值</span></span><br><span class="line"><span class="keyword">var</span> names = [<span class="number">2</span>]<span class="type">string</span>&#123;<span class="string">&quot;libai&quot;</span>, <span class="string">&quot;wangwei&quot;</span>&#125;</span><br><span class="line"><span class="comment">// 方式3: 声明+赋值(指定位置)</span></span><br><span class="line"><span class="keyword">var</span> ages = [<span class="number">3</span>]<span class="type">int</span>&#123;<span class="number">0</span>: <span class="number">87</span>, <span class="number">2</span>: <span class="number">99</span>&#125; <span class="comment">// 此时age[1]=0</span></span><br><span class="line">fmt.Println(names, ages)</span><br><span class="line"><span class="comment">// 方式4: 省略个数</span></span><br><span class="line"><span class="keyword">var</span> addrs = [...]<span class="type">string</span>&#123;<span class="string">&quot;a&quot;</span>, <span class="string">&quot;b&quot;</span>, <span class="string">&quot;c&quot;</span>&#125;</span><br><span class="line"><span class="keyword">var</span> addrs2 = [...]<span class="type">string</span>&#123;<span class="number">0</span>: <span class="string">&quot;a&quot;</span>, <span class="number">2</span>: <span class="string">&quot;b&quot;</span>&#125;</span><br><span class="line">fmt.Println(addrs, addrs2)</span><br></pre></td></tr></table></figure>

<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 声明指针类型的数组, 不会开辟内存初始化数组中的值, numbsers=nil</span></span><br><span class="line"><span class="keyword">var</span> numbers *[<span class="number">3</span>]<span class="type">int</span></span><br><span class="line"><span class="comment">// 声明数组并初始化, 返回指针类型的数组(指针)</span></span><br><span class="line">numbers := <span class="built_in">new</span>([<span class="number">3</span>]<span class="type">int</span>)</span><br></pre></td></tr></table></figure>

<h4 id="3-5-2-内存管理"><a href="#3-5-2-内存管理" class="headerlink" title="3.5.2 内存管理"></a>3.5.2 内存管理</h4><ul>
<li>数组的内存是连续的</li>
<li>数组的内存地址实际上就是数组第一个元素的内存地址</li>
<li>对于字符串的内部存储: len + pointer</li>
</ul>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 差1个字节</span></span><br><span class="line">nums := [<span class="number">3</span>]<span class="type">int8</span>&#123;<span class="number">11</span>, <span class="number">22</span>, <span class="number">33</span>&#125;</span><br><span class="line">fmt.Printf(<span class="string">&quot;数组的内存地址是: %p \n&quot;</span>, &amp;nums)</span><br><span class="line"><span class="comment">// 数组的内存地址是: 0xc00001c0b8</span></span><br><span class="line">fmt.Printf(<span class="string">&quot;数组的第1个元素的内存地址是: %p \n&quot;</span>, &amp;nums[<span class="number">0</span>])</span><br><span class="line"><span class="comment">// 数组的第1个元素的内存地址是: 0xc00001c0b8</span></span><br><span class="line">fmt.Printf(<span class="string">&quot;数组的第2个元素的内存地址是: %p \n&quot;</span>, &amp;nums[<span class="number">1</span>])</span><br><span class="line"><span class="comment">// 数组的第2个元素的内存地址是: 0xc00001c0b9</span></span><br><span class="line">fmt.Printf(<span class="string">&quot;数组的第3个元素的内存地址是: %p \n&quot;</span>, &amp;nums[<span class="number">2</span>])</span><br><span class="line"><span class="comment">// 数组的第3个元素的内存地址是: 0xc00001c0ba</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 差4个字节</span></span><br><span class="line">nums := [<span class="number">3</span>]<span class="type">int32</span>&#123;<span class="number">11</span>, <span class="number">22</span>, <span class="number">33</span>&#125;</span><br><span class="line">fmt.Printf(<span class="string">&quot;数组的内存地址是: %p \n&quot;</span>, &amp;nums)</span><br><span class="line"><span class="comment">// 数组的内存地址是: 0xc00001c0b8</span></span><br><span class="line">fmt.Printf(<span class="string">&quot;数组的第1个元素的内存地址是: %p \n&quot;</span>, &amp;nums[<span class="number">0</span>])</span><br><span class="line"><span class="comment">// 数组的第1个元素的内存地址是: 0xc00001c0b8</span></span><br><span class="line">fmt.Printf(<span class="string">&quot;数组的第2个元素的内存地址是: %p \n&quot;</span>, &amp;nums[<span class="number">1</span>])</span><br><span class="line"><span class="comment">// 数组的第2个元素的内存地址是: 0xc00001c0b9</span></span><br><span class="line">fmt.Printf(<span class="string">&quot;数组的第3个元素的内存地址是: %p \n&quot;</span>, &amp;nums[<span class="number">2</span>])</span><br><span class="line"><span class="comment">// 数组的第3个元素的内存地址是: 0xc00001c0ba</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 差16个字节</span></span><br><span class="line">names := []<span class="type">string</span>&#123;<span class="string">&quot;李白&quot;</span>, <span class="string">&quot;abcd&quot;</span>&#125;</span><br><span class="line">fmt.Printf(<span class="string">&quot;数组的内存地址是: %p \n&quot;</span>, &amp;names)</span><br><span class="line"><span class="comment">// 数组的内存地址是: 0xc000008078</span></span><br><span class="line">fmt.Printf(<span class="string">&quot;数组的第1个元素的内存地址是: %p \n&quot;</span>, &amp;names[<span class="number">0</span>])</span><br><span class="line"><span class="comment">// 数组的第1个元素的内存地址是: 0xc0000623c0</span></span><br><span class="line">fmt.Printf(<span class="string">&quot;数组的第2个元素的内存地址是: %p \n&quot;</span>, &amp;names[<span class="number">1</span>])</span><br><span class="line"><span class="comment">// 数组的第2个元素的内存地址是: 0xc0000623d0</span></span><br></pre></td></tr></table></figure>

<h4 id="3-5-3-可变和拷贝"><a href="#3-5-3-可变和拷贝" class="headerlink" title="3.5.3 可变和拷贝"></a>3.5.3 可变和拷贝</h4><p>可变, 数组的元素可被更改(长度和类型都不可被修改)</p>
<p>注意: 字符串不可被修改</p>
<p>拷贝, 变量赋值时会重新拷贝</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">name1 := [<span class="number">2</span>]<span class="type">string</span>(<span class="string">&quot;李白&quot;</span>, <span class="string">&quot;abcd&quot;</span>)</span><br><span class="line">name2 := name1</span><br><span class="line"></span><br><span class="line">name1[<span class="number">1</span>] = <span class="string">&quot;qwer&quot;</span></span><br><span class="line">fmt.Println(name1, name2)</span><br><span class="line"><span class="comment">// [李白, qwer] [李白, abcd]</span></span><br></pre></td></tr></table></figure>

<h4 id="3-5-4-长度索引切片与循环"><a href="#3-5-4-长度索引切片与循环" class="headerlink" title="3.5.4 长度索引切片与循环"></a>3.5.4 长度索引切片与循环</h4><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 1. 长度</span></span><br><span class="line">name := [<span class="number">2</span>]<span class="type">string</span>&#123;<span class="string">&quot;李白&quot;</span>, <span class="string">&quot;abcd&quot;</span>&#125;</span><br><span class="line">fmt.Println(<span class="built_in">len</span>(name))</span><br><span class="line"><span class="comment">// 2. 索引</span></span><br><span class="line">fmt.Println(name[<span class="number">0</span>])</span><br><span class="line">name[<span class="number">1</span>] = <span class="string">&quot;qwer&quot;</span></span><br><span class="line">fmt.Println(name[<span class="number">1</span>])</span><br><span class="line"><span class="comment">// 3. 切片</span></span><br><span class="line">nums := [<span class="number">4</span>]<span class="type">int</span>&#123;<span class="number">12</span>, <span class="number">23</span>, <span class="number">34</span>, <span class="number">45</span>&#125;</span><br><span class="line">data := nums[<span class="number">0</span>:<span class="number">2</span>] <span class="comment">// 前闭后开</span></span><br><span class="line">fmt.Println(data)</span><br><span class="line"><span class="comment">// 4. 循环</span></span><br><span class="line"><span class="keyword">for</span> i := <span class="number">0</span>; i &lt; <span class="built_in">len</span>(name); i++ &#123;</span><br><span class="line">    fmt.Println(i, nums[i])</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">for</span> key, item := <span class="keyword">range</span> name &#123;</span><br><span class="line">    fmt.Println(key, item)</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">for</span> key := <span class="keyword">range</span> name &#123;</span><br><span class="line">    fmt.Println(key)</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">for</span> _, item := <span class="keyword">range</span> name &#123;</span><br><span class="line">    fmt.Println(item)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="3-5-5-数组嵌套"><a href="#3-5-5-数组嵌套" class="headerlink" title="3.5.5 数组嵌套"></a>3.5.5 数组嵌套</h4><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> nestData [<span class="number">2</span>][<span class="number">3</span>]<span class="type">int</span></span><br><span class="line">nestData[<span class="number">0</span>] = [<span class="number">3</span>]<span class="type">int</span>&#123;<span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>&#125;</span><br><span class="line">nestData[<span class="number">0</span>][<span class="number">0</span>] = <span class="number">10</span></span><br><span class="line"><span class="keyword">for</span> i := <span class="number">0</span>; i &lt; <span class="built_in">len</span>(nestData); i++ &#123;</span><br><span class="line">    <span class="keyword">for</span> j := <span class="number">0</span>; j &lt; <span class="built_in">len</span>(nestData[i]); j++ &#123;</span><br><span class="line">        nestData[i][j] += (i + <span class="number">1</span>) * (j + <span class="number">1</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">fmt.Println(nestData)</span><br><span class="line"><span class="comment">// 声明+赋值</span></span><br><span class="line">nestData = [<span class="number">2</span>][<span class="number">3</span>]<span class="type">int</span>&#123;[<span class="number">3</span>]<span class="type">int</span>&#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>&#125;, [<span class="number">3</span>]<span class="type">int</span>&#123;<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>&#125;&#125;</span><br><span class="line">fmt.Println(nestData)</span><br></pre></td></tr></table></figure>

<h3 id="3-6-切片"><a href="#3-6-切片" class="headerlink" title="3.6 切片"></a>3.6 切片</h3><blockquote>
<p>表示多个数据(数据集合), 可以理解为动态数组</p>
</blockquote>
<h4 id="3-6-1-切片的存储"><a href="#3-6-1-切片的存储" class="headerlink" title="3.6.1 切片的存储"></a>3.6.1 切片的存储</h4><p>切片是Go中重要的数据类型, 每个切片内部都维护着: 数组指针, 切片长度, 切片容量 三个数据</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">type</span> slice <span class="keyword">struct</span> &#123;</span><br><span class="line">    array unsafe.Pointer</span><br><span class="line">    <span class="built_in">len</span> <span class="type">int</span></span><br><span class="line">    <span class="built_in">cap</span> <span class="type">int</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>切片的自动扩容: 在向切片中追加的数据大于容量时, 内部会自动扩容且每次扩容都上当前容量的2倍(当容量超过1024时, 每次扩容值增加1&#x2F;4容量)</p>
<h4 id="3-6-2-创建切片"><a href="#3-6-2-创建切片" class="headerlink" title="3.6.2 创建切片"></a>3.6.2 创建切片</h4><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> nums []<span class="type">int</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// make只用于切片, 字典, channel(推荐)</span></span><br><span class="line"><span class="keyword">var</span> users = <span class="built_in">make</span>([]<span class="type">int</span>,<span class="number">1</span>,<span class="number">3</span>)</span><br><span class="line"><span class="comment">// &#123;0&#125; len=1, cap=3</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> data = []<span class="type">int</span>&#123;<span class="number">11</span>,<span class="number">22</span>,<span class="number">33</span>&#125;</span><br><span class="line"><span class="comment">// 简写: (推荐)</span></span><br><span class="line">data := []<span class="type">int</span>&#123;<span class="number">11</span>,<span class="number">22</span>,<span class="number">33</span>&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 创建切片指针类型</span></span><br><span class="line"><span class="keyword">var</span> v1 = <span class="built_in">new</span>([]<span class="type">int</span>)</span><br><span class="line"><span class="comment">// 指向nil</span></span><br><span class="line"><span class="keyword">var</span> v2 *[]<span class="type">int</span></span><br></pre></td></tr></table></figure>

<h4 id="3-6-3-切片的自动扩容"><a href="#3-6-3-切片的自动扩容" class="headerlink" title="3.6.3 切片的自动扩容"></a>3.6.3 切片的自动扩容</h4><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">v3 := []<span class="type">int</span>&#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>&#125;</span><br><span class="line">v4 := <span class="built_in">append</span>(v3, <span class="number">4</span>)</span><br><span class="line">fmt.Println(v3, <span class="built_in">len</span>(v3), <span class="built_in">cap</span>(v3)) <span class="comment">//[1 2 3] 3 3</span></span><br><span class="line">v3[<span class="number">0</span>] = <span class="number">5</span></span><br><span class="line">fmt.Println(v3, <span class="built_in">len</span>(v3), <span class="built_in">cap</span>(v3)) <span class="comment">//[5 2 3] 3 3</span></span><br><span class="line">fmt.Println(v4, <span class="built_in">len</span>(v4), <span class="built_in">cap</span>(v4)) <span class="comment">//[1 2 3 4] 4 6</span></span><br></pre></td></tr></table></figure>

<h4 id="3-6-4-变量赋值"><a href="#3-6-4-变量赋值" class="headerlink" title="3.6.4 变量赋值"></a>3.6.4 变量赋值</h4><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 切片赋值</span></span><br><span class="line">v1 := []<span class="type">int</span>&#123;<span class="number">6</span>, <span class="number">9</span>&#125;</span><br><span class="line">v2 := v1</span><br><span class="line"><span class="comment">// 字符串内部元素是不可以被修改的(原子性操作)</span></span><br><span class="line">fmt.Printf(<span class="string">&quot;v1: %p \n&quot;</span>, &amp;v1) <span class="comment">// 0xc00001c0a8</span></span><br><span class="line">fmt.Printf(<span class="string">&quot;v2: %p \n&quot;</span>, &amp;v2) <span class="comment">// 0xc00001c0c0</span></span><br><span class="line">v1[<span class="number">0</span>] = <span class="number">11111</span></span><br><span class="line">fmt.Println(v1, v2)</span><br><span class="line"><span class="comment">// [11111 9] [11111 9]</span></span><br></pre></td></tr></table></figure>

<p>如果扩容, 则内部存储数据的数组就会重新开辟区域</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">v1 := []<span class="type">int</span>&#123;<span class="number">6</span>, <span class="number">9</span>&#125;</span><br><span class="line">v2 := v1</span><br><span class="line"><span class="comment">// 字符串内部元素是不可以被修改的(原子性操作)</span></span><br><span class="line">fmt.Printf(<span class="string">&quot;v1: %p \n&quot;</span>, &amp;v1) <span class="comment">// 0xc00001c0a8</span></span><br><span class="line">fmt.Printf(<span class="string">&quot;v2: %p \n&quot;</span>, &amp;v2) <span class="comment">// 0xc00001c0c0</span></span><br><span class="line">v1 = <span class="built_in">append</span>(v1, <span class="number">123</span>)</span><br><span class="line">fmt.Println(v1, v2)</span><br><span class="line"><span class="comment">// [6 9 123] [6 9]</span></span><br></pre></td></tr></table></figure>

<p>总结:<br>目前所学的所有数据类型中, 在修改切片内部的元素时候, 会造成所有赋值的变量同时修改(不扩容)<br>拓展: <code>引用类型和值类型</code></p>
<h3 id="3-7-字典类型-Map"><a href="#3-7-字典类型-Map" class="headerlink" title="3.7 字典类型(Map)"></a>3.7 字典类型(Map)</h3><blockquote>
<p>字典类型最大的特点是查找速度非常快, 因为其底层存储基于哈希表存储</p>
</blockquote>
<p>Map的特点:</p>
<ul>
<li>键不能重复</li>
<li>键必须可哈希(可哈希的有: int,bool,float,string,array)</li>
<li>无序</li>
</ul>
<h4 id="3-7-1-声明-amp-初始化"><a href="#3-7-1-声明-amp-初始化" class="headerlink" title="3.7.1 声明&amp;初始化"></a>3.7.1 声明&amp;初始化</h4><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">userInfo := <span class="keyword">map</span>[<span class="type">string</span>]<span class="type">string</span>&#123;</span><br><span class="line">    <span class="string">&quot;name&quot;</span>: <span class="string">&quot;李白&quot;</span>,</span><br><span class="line">    <span class="string">&quot;age&quot;</span>:  <span class="string">&quot;18&quot;</span>&#125;</span><br><span class="line">fmt.Println(userInfo)</span><br><span class="line"><span class="comment">// map[age:18 name:李白]</span></span><br><span class="line">fmt.Println(userInfo[<span class="string">&quot;name&quot;</span>])</span><br><span class="line"><span class="comment">// 李白</span></span><br><span class="line">userInfo[<span class="string">&quot;age&quot;</span>] = <span class="string">&quot;20&quot;</span></span><br><span class="line">userInfo[<span class="string">&quot;email&quot;</span>] = <span class="string">&quot;123456&quot;</span></span><br><span class="line">fmt.Println(userInfo[<span class="string">&quot;age&quot;</span>])</span><br><span class="line"><span class="comment">// 20</span></span><br><span class="line">fmt.Println(userInfo)</span><br><span class="line"><span class="comment">// map[age:20 email:123456 name:李白]</span></span><br></pre></td></tr></table></figure>

<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// data := make(map[int]int,10)</span></span><br><span class="line">data := <span class="built_in">make</span>(<span class="keyword">map</span>[<span class="type">int</span>]<span class="type">int</span>)</span><br><span class="line">data[<span class="number">1</span>] = <span class="number">123</span></span><br><span class="line">data[<span class="number">2</span>] = <span class="number">1234</span></span><br><span class="line">data[<span class="number">3</span>] = <span class="number">123456</span></span><br><span class="line">fmt.Println(data)</span><br><span class="line"><span class="comment">// map[1:123 2:1234 3:123456]</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 声明(不可以单独赋值, 只能整体赋值)</span></span><br><span class="line"><span class="keyword">var</span> row <span class="keyword">map</span>[<span class="type">int</span>]<span class="type">int</span></span><br><span class="line">row = data</span><br><span class="line">fmt.Println(row)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 声明, 也是用于整体赋值</span></span><br><span class="line">val := <span class="built_in">new</span>(<span class="keyword">map</span>[<span class="type">int</span>]<span class="type">int</span>)</span><br><span class="line">val = &amp;data</span><br><span class="line">fmt.Println(val)</span><br><span class="line"><span class="comment">// &amp;map[1:123 2:1234 3:123456]</span></span><br></pre></td></tr></table></figure>

<h4 id="3-7-2-map的常用操作"><a href="#3-7-2-map的常用操作" class="headerlink" title="3.7.2 map的常用操作"></a>3.7.2 map的常用操作</h4><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 1. 获取map长度 (键值对的个数)</span></span><br><span class="line">data := <span class="keyword">map</span>[<span class="type">string</span>]<span class="type">int</span>&#123;<span class="string">&quot;李白&quot;</span>: <span class="number">12</span>, <span class="string">&quot;张三&quot;</span>: <span class="number">23</span>&#125;</span><br><span class="line">fmt.Println(<span class="built_in">len</span>(data)) <span class="comment">// 2</span></span><br><span class="line">fmt.Println(data)      <span class="comment">// map[张三:23 李白:12]</span></span><br><span class="line"><span class="comment">// 2. 根据参数值计算出一个合适的容量, 即至少留有10个键值对的存储空间</span></span><br><span class="line">info := <span class="built_in">make</span>(<span class="keyword">map</span>[<span class="type">string</span>]<span class="type">int</span>, <span class="number">10</span>)</span><br><span class="line">info[<span class="string">&quot;李白&quot;</span>] = <span class="number">123</span></span><br><span class="line">info[<span class="string">&quot;张三&quot;</span>] = <span class="number">234</span></span><br><span class="line"><span class="comment">// fmt.Println(cap(info)) , 会报错</span></span><br><span class="line">fmt.Println(info)</span><br><span class="line"><span class="comment">// 3. 嵌套</span></span><br><span class="line">v1 := <span class="built_in">make</span>(<span class="keyword">map</span>[<span class="type">string</span>]<span class="keyword">map</span>[<span class="type">string</span>]<span class="type">int</span>)</span><br><span class="line">v1[<span class="string">&quot;李白&quot;</span>] = <span class="keyword">map</span>[<span class="type">string</span>]<span class="type">int</span>&#123;<span class="string">&quot;age&quot;</span>: <span class="number">18</span>&#125;</span><br><span class="line">fmt.Println(v1)</span><br><span class="line">fmt.Println(v1[<span class="string">&quot;李白&quot;</span>][<span class="string">&quot;age&quot;</span>])</span><br><span class="line">v2 := <span class="built_in">make</span>(<span class="keyword">map</span>[<span class="type">string</span>][<span class="number">2</span>]<span class="keyword">map</span>[<span class="type">string</span>]<span class="type">int</span>)</span><br><span class="line">v2[<span class="string">&quot;张三&quot;</span>] = [<span class="number">2</span>]<span class="keyword">map</span>[<span class="type">string</span>]<span class="type">int</span>&#123;&#123;<span class="string">&quot;age&quot;</span>: <span class="number">18</span>&#125;, &#123;<span class="string">&quot;sal&quot;</span>: <span class="number">2000</span>&#125;&#125;</span><br><span class="line">fmt.Println(v2)</span><br><span class="line"><span class="comment">// 4. 增删改查</span></span><br><span class="line"><span class="comment">// 增加, 如果是已存在的key则是修改</span></span><br><span class="line">v1[<span class="string">&quot;张飞&quot;</span>] = <span class="keyword">map</span>[<span class="type">string</span>]<span class="type">int</span>&#123;<span class="string">&quot;age&quot;</span>: <span class="number">32</span>&#125;</span><br><span class="line"><span class="comment">// 删除</span></span><br><span class="line"><span class="built_in">delete</span>(v1, <span class="string">&quot;张飞&quot;</span>)</span><br><span class="line"><span class="comment">// 查看</span></span><br><span class="line"><span class="comment">// data[&quot;张飞&quot;]</span></span><br><span class="line"><span class="comment">// 循环所有查看</span></span><br><span class="line"><span class="keyword">for</span> key, val := <span class="keyword">range</span> v1 &#123;</span><br><span class="line">    fmt.Println(key, val)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 切片与map不可哈希</span></span><br></pre></td></tr></table></figure>

<h4 id="3-7-3-map底层原理剖析"><a href="#3-7-3-map底层原理剖析" class="headerlink" title="3.7.3 map底层原理剖析"></a>3.7.3 map底层原理剖析</h4><p>Golang中的map有自己的一套实现原理, 核心是hmap和bmap两个结构体</p>
<p><img src="https://gitee-blog-1317332932.cos.ap-nanjing.myqcloud.com/imgs/img.png" alt="img.png"></p>
<h5 id="3-7-3-1-初始化"><a href="#3-7-3-1-初始化" class="headerlink" title="3.7.3.1 初始化"></a>3.7.3.1 初始化</h5><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 初始化一个可容纳10个元素的map</span></span><br><span class="line">info := <span class="built_in">make</span>(<span class="keyword">map</span>[<span class="type">string</span>]<span class="type">int</span>, <span class="number">10</span>)</span><br></pre></td></tr></table></figure>

<ul>
<li>第一步: 创建一个hmap结构体对象</li>
<li>第二步: 生成一个哈希因子hash0 并赋值到hmap对象中(用于后续为key创建哈希值)</li>
<li>第三步: 根据hint&#x3D;10, 并根据算法规则创建B, 此处B&#x3D;1<ul>
<li>hint    B</li>
<li>0-8     0</li>
<li>9-13    1</li>
<li>14-26   2</li>
<li>…</li>
</ul>
</li>
<li>第四步: 根据B创建桶(bmap对象), 并存放在buckets数组中, 此处bmap&#x3D;2<ul>
<li>当B&lt;4时, 创建桶个数为: 2^B</li>
<li>当B&gt;&#x3D;4时,创建桶个数为: 2^B + 2^(B-4) (标准桶+溢出桶)</li>
</ul>
</li>
</ul>
<p>注意: 每个bmap中可以存储8个键值对, 当不够存储时需要使用溢出桶, 并将当前bmap中的overflow字段指向溢出桶的位置</p>
<h5 id="3-7-3-2-写入数据"><a href="#3-7-3-2-写入数据" class="headerlink" title="3.7.3.2 写入数据"></a>3.7.3.2 写入数据</h5><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">info[<span class="string">&quot;name&quot;</span>] = <span class="string">&quot;李白&quot;</span></span><br></pre></td></tr></table></figure>

<p>在map中写入数据时, 内部的执行流程为:</p>
<ul>
<li>第一步: 结合哈希因子hash0和键name生成哈希值</li>
<li>第二步: 获取哈希值得后B位, 并根据后B位的值决定该键值对存放到哪个桶中(bmap)<ul>
<li>将哈希值与桶掩码(B个为1的二进制码组)进行&amp;运算, 最终得到哈希值后B位的值</li>
<li>找桶的原则实际上是根据后B位的位运算计算出索引的位置, 然后再去buckets数组中根据索引找到目标桶(bmap)</li>
</ul>
</li>
<li>第三步: 确定桶之后, 在桶中写入数据<ul>
<li>获取哈希值的tophash(哈希值的高8位), 将tophash, key, value 分别写入到桶的三个数组中</li>
<li>如果桶已满, 则通过overflow找到溢出桶, 并在溢出桶中继续写入</li>
<li>注意: 在桶中查找数据时, 会基于tophash来寻找(tiohash相同时再去比较key)</li>
</ul>
</li>
<li>第四步: hmap的个数count++(map中的元素个数+1)</li>
</ul>
<h5 id="3-7-3-3-读取数据"><a href="#3-7-3-3-读取数据" class="headerlink" title="3.7.3.3 读取数据"></a>3.7.3.3 读取数据</h5><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">value := info[<span class="string">&quot;name&quot;</span>]</span><br></pre></td></tr></table></figure>

<p>在map中读取数据时, 内部的执行流程为:</p>
<ul>
<li>第一步: 结合哈希因子和键name生成哈希值</li>
<li>第二步: 获取哈希值的后B位, 并根据后B位的值找到存储当前键值对的桶</li>
<li>第三步: 再根据key的哈希值得出tophash(高8位), 根据tophash和key去桶中查找数据<ul>
<li>当前桶如果没有找到, 则根据overflow再去溢出桶中找, 均未找到则表示key不存在</li>
</ul>
</li>
</ul>
<h5 id="3-7-3-4-扩容"><a href="#3-7-3-4-扩容" class="headerlink" title="3.7.3.4 扩容"></a>3.7.3.4 扩容</h5><p>在向map中添加数据时, 当达到某个条件, 则会引起字典扩容</p>
<p>扩容条件:</p>
<ul>
<li>map中数据总个数 &#x2F; 桶个数 &gt; 6.5, 引发翻倍扩容</li>
<li>使用太多的溢出桶(溢出桶使用太多会导致map处理速度降低)<ul>
<li>B &lt;&#x3D; 15, 已使用溢出桶个数 &gt;&#x3D; 2^B时, 引发等量扩容</li>
<li>B &gt; 15, 已使用溢出桶个数 &gt; 2^15时, 引发等量扩容</li>
</ul>
</li>
</ul>
<p><img src="https://gitee-blog-1317332932.cos.ap-nanjing.myqcloud.com/imgs/img_1.png" alt="img.png"></p>
<h5 id="3-7-3-5-迁移"><a href="#3-7-3-5-迁移" class="headerlink" title="3.7.3.5 迁移"></a>3.7.3.5 迁移</h5><p>扩容之后, 必然会涉及到数据的迁移, 即将旧桶中的数据迁移到新桶中</p>
<ol>
<li>翻倍扩容</li>
</ol>
<p>翻倍扩容的迁移规则: 将旧桶中的数据分流到新的两个桶中(比例不定, 根据哈希值动态分配), 并且桶编号为: 桶编号位置, 翻倍后对应编号位置</p>
<p><img src="https://gitee-blog-1317332932.cos.ap-nanjing.myqcloud.com/imgs/img_2.png" alt="img.png"></p>
<p>新桶个数是旧桶个数的2倍, 即: map中的B+&#x3D;1. </p>
<p>迁移时会遍历某个旧桶中所有的key(包括溢出桶), 并根据key重新生成哈希值, 根据哈希值的低B位决定该键值对分流到哪个新桶中.</p>
<ol start="2">
<li>等量扩容</li>
</ol>
<p>如果是等量扩容(由于溢出桶太多引发的扩容), 数据迁移机制会比较简单: 将旧桶(包括溢出桶)中的值迁移到新桶中</p>
<p>这种扩容, 迁移的方法的意义在于: 当溢出桶较多, 而每个桶中的数据不多时, 可以通过等量扩容和迁移让数据更加紧凑, 从而减少溢出桶</p>
<h3 id="3-8-指针"><a href="#3-8-指针" class="headerlink" title="3.8 指针"></a>3.8 指针</h3><h4 id="3-8-1-指针的定义"><a href="#3-8-1-指针的定义" class="headerlink" title="3.8.1 指针的定义"></a>3.8.1 指针的定义</h4><p>指针是一种数据类型, 用于表示数据的内存地址</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 声明一个string类型的变量 (默认初始化为空字符串)</span></span><br><span class="line"><span class="keyword">var</span> v1 <span class="type">string</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 声明一个string的指针类型的变量 (默认初始化为nil)</span></span><br><span class="line"><span class="keyword">var</span> v2 *<span class="type">string</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 声明一个string类型的变量为&quot;李白&quot;</span></span><br><span class="line"><span class="keyword">var</span> name <span class="type">string</span> = <span class="string">&quot;李白&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 声明一个string的指针类型的变量, 值为name对应的内存地址</span></span><br><span class="line"><span class="keyword">var</span> pointer *<span class="type">string</span> = &amp;name</span><br></pre></td></tr></table></figure>

<p>指针的意义: 指针相当于创建了一个地址的引用, 便于后续再次向此地址进行请求</p>
<h4 id="3-8-2-指针的应用场景"><a href="#3-8-2-指针的应用场景" class="headerlink" title="3.8.2 指针的应用场景"></a>3.8.2 指针的应用场景</h4><p>场景1: 函数传参</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> <span class="string">&quot;fmt&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">changeData</span><span class="params">(ptr *<span class="type">string</span>)</span></span> &#123;</span><br><span class="line">    *ptr = <span class="string">&quot;HaHa&quot;</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">    name := <span class="string">&quot;libai&quot;</span></span><br><span class="line">    <span class="comment">// 找到对于内存的值并修改</span></span><br><span class="line">    changeData(&amp;name)</span><br><span class="line">    fmt.Println(name)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>场景2: 用户输入</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> <span class="string">&quot;fmt&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">    <span class="keyword">var</span> userName <span class="type">string</span></span><br><span class="line">    fmt.Println(<span class="string">&quot;请输入用户名: &quot;</span>)</span><br><span class="line">	<span class="comment">// 将用户输入的值, 赋值给内存地址对应的区域中</span></span><br><span class="line">    fmt.Scanf(<span class="string">&quot;%s&quot;</span>, &amp;userName)</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">if</span> userName == <span class="string">&quot;李白&quot;</span> &#123;</span><br><span class="line">        fmt.Println(<span class="string">&quot;登陆成功!&quot;</span>)</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        fmt.Println(<span class="string">&quot;登陆失败!&quot;</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="3-8-3-补充"><a href="#3-8-3-补充" class="headerlink" title="3.8.3 补充"></a>3.8.3 补充</h4><ul>
<li>数组的地址 &#x3D; 数组第一个元素的地址</li>
</ul>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> data [<span class="number">3</span>]<span class="type">int</span></span><br><span class="line">data = [<span class="number">3</span>]<span class="type">int</span>&#123;<span class="number">11</span>, <span class="number">22</span>, <span class="number">33</span>&#125;</span><br><span class="line">fmt.Printf(<span class="string">&quot;数组的地址: %p, 数组第一个元素的地址: %p \n&quot;</span>, &amp;data, &amp;data[<span class="number">0</span>])</span><br><span class="line"><span class="comment">// 数组的地址: 0xc000010138, 数组第一个元素的地址: 0xc000010138</span></span><br><span class="line"><span class="comment">// 两者的数据类型不同</span></span><br><span class="line">fmt.Println(<span class="string">&quot;&amp;data type: &quot;</span>, reflect.TypeOf(&amp;data))</span><br><span class="line"><span class="comment">// &amp;data type:  *[3]int</span></span><br><span class="line">fmt.Println(<span class="string">&quot;&amp;data[0] type: &quot;</span>, reflect.TypeOf(&amp;data[<span class="number">0</span>]))</span><br><span class="line"><span class="comment">// &amp;data type:  *[3]int</span></span><br></pre></td></tr></table></figure>

<ul>
<li>指针的计算</li>
</ul>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">data := [<span class="number">3</span>]<span class="type">int8</span>&#123;<span class="number">11</span>, <span class="number">22</span>, <span class="number">33</span>&#125;</span><br><span class="line"><span class="comment">// 1. 获取第一个元素的地址</span></span><br><span class="line"><span class="keyword">var</span> firstDataPtr *<span class="type">int8</span> = &amp;data[<span class="number">0</span>]</span><br><span class="line"><span class="comment">// 2. 转换成Pointer类型</span></span><br><span class="line">ptr := unsafe.Pointer(firstDataPtr)</span><br><span class="line"><span class="comment">// 3. 转换为uintptr类型, 再进行内存地址计算</span></span><br><span class="line">tarPtr := <span class="type">uintptr</span>(ptr) + <span class="number">1</span></span><br><span class="line"><span class="comment">// 4. 根据新地址, 重新转换成Pointer类型</span></span><br><span class="line">newPtr := unsafe.Pointer(tarPtr)</span><br><span class="line"><span class="comment">// 5. Pointer对象转换为int8指针类型</span></span><br><span class="line">value := (*<span class="type">int8</span>)(newPtr)</span><br><span class="line"><span class="comment">// 6. 根据指针获取值</span></span><br><span class="line">fmt.Println(<span class="string">&quot;最终结果为: &quot;</span>, *value)</span><br></pre></td></tr></table></figure>

<h3 id="3-9-结构体"><a href="#3-9-结构体" class="headerlink" title="3.9 结构体"></a>3.9 结构体</h3><blockquote>
<p>结构体是一个复合数据类型, 用于表示一组数据<br>结构体由一系列属性组成, 每个属性都有自己的类型和值</p>
</blockquote>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 定义</span></span><br><span class="line"><span class="keyword">type</span> Person <span class="keyword">struct</span> &#123;</span><br><span class="line">    name <span class="type">string</span></span><br><span class="line">    age <span class="type">int</span></span><br><span class="line">    email <span class="type">string</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 初始化</span></span><br><span class="line"><span class="keyword">var</span> p1 = Person&#123;<span class="string">&quot;李白&quot;</span>, <span class="number">20</span>, <span class="string">&quot;123@qq.com&quot;</span>&#125;</span><br><span class="line"><span class="comment">// 从结构体中取值</span></span><br><span class="line">fmt.Println(p1.name, p1.age, p1.email)</span><br></pre></td></tr></table></figure>

<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">type</span> 结构体名称 <span class="keyword">struct</span> &#123;</span><br><span class="line">    字段 类型</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="3-9-1-结构体定义"><a href="#3-9-1-结构体定义" class="headerlink" title="3.9.1 结构体定义"></a>3.9.1 结构体定义</h4><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 1</span></span><br><span class="line"><span class="keyword">type</span> Person <span class="keyword">struct</span> &#123;</span><br><span class="line">    name <span class="type">string</span></span><br><span class="line">    age <span class="type">int</span></span><br><span class="line">    hooby []<span class="type">string</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 2</span></span><br><span class="line"><span class="keyword">type</span> Address <span class="keyword">struct</span> &#123;</span><br><span class="line">    city, state <span class="type">string</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 3</span></span><br><span class="line"><span class="keyword">type</span> Person <span class="keyword">struct</span> &#123;</span><br><span class="line">    name <span class="type">string</span></span><br><span class="line">    age <span class="type">int</span></span><br><span class="line">    ad Address</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 4</span></span><br><span class="line"><span class="keyword">type</span> Person <span class="keyword">struct</span> &#123;</span><br><span class="line">    name <span class="type">string</span></span><br><span class="line">    age <span class="type">int</span></span><br><span class="line">    Address <span class="comment">// 匿名字段 相当于: Address Address</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="3-9-2-初始化"><a href="#3-9-2-初始化" class="headerlink" title="3.9.2 初始化"></a>3.9.2 初始化</h4><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">type</span> Person <span class="keyword">struct</span> &#123;</span><br><span class="line">    name  <span class="type">string</span></span><br><span class="line">    age   <span class="type">int</span></span><br><span class="line">    hobby []<span class="type">string</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 方式1:</span></span><br><span class="line"><span class="keyword">var</span> p1 = Person&#123;<span class="string">&quot;李白&quot;</span>, <span class="number">18</span>, []<span class="type">string</span>&#123;<span class="string">&quot;写诗&quot;</span>, <span class="string">&quot;喝酒&quot;</span>&#125;&#125;</span><br><span class="line">fmt.Println(p1)</span><br><span class="line"><span class="comment">// 方式2:</span></span><br><span class="line"><span class="keyword">var</span> p2 = Person&#123;</span><br><span class="line">    name:  <span class="string">&quot;张飞&quot;</span>,</span><br><span class="line">    age:   <span class="number">20</span>,</span><br><span class="line">    hobby: []<span class="type">string</span>&#123;<span class="string">&quot;哥哥&quot;</span>&#125;,</span><br><span class="line">&#125;</span><br><span class="line">fmt.Println(p2)</span><br><span class="line"><span class="comment">// 方式3:</span></span><br><span class="line"><span class="keyword">var</span> p3 Person</span><br><span class="line">p3.name = <span class="string">&quot;李商隐&quot;</span></span><br><span class="line">p3.age = <span class="number">58</span></span><br><span class="line">p3.hobby = []<span class="type">string</span>&#123;<span class="string">&quot;走路&quot;</span>&#125;</span><br><span class="line">fmt.Println(p3)</span><br></pre></td></tr></table></figure>

<p>赋值拷贝:<br>对于所有类型都是拷贝了, 只不过有的是拷贝数值, 有的是拷贝地址</p>
<ul>
<li>值: int,string,…,数组 </li>
<li>址: map, 切片</li>
</ul>
<p>注意:<br>对于默认拷贝(值拷贝)的情况, 可以将其改变为指针类型, 使得数据实现同步修改</p>
<h4 id="3-9-3-结构体标签"><a href="#3-9-3-结构体标签" class="headerlink" title="3.9.3 结构体标签"></a>3.9.3 结构体标签</h4><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">type</span> Person <span class="keyword">struct</span> &#123;</span><br><span class="line">    name <span class="type">string</span> <span class="string">&quot;姓名&quot;</span></span><br><span class="line">    age  <span class="type">int</span>    <span class="string">&quot;年龄&quot;</span></span><br><span class="line">&#125;</span><br><span class="line">p1 := Person&#123;</span><br><span class="line">    name: <span class="string">&quot;李白&quot;</span>,</span><br><span class="line">    age:  <span class="number">20</span>,</span><br><span class="line">&#125;</span><br><span class="line">p1Type := reflect.TypeOf(p1)</span><br><span class="line"><span class="comment">// 方法1</span></span><br><span class="line"><span class="keyword">for</span> i := <span class="number">0</span>; i &lt; <span class="number">2</span>; i++ &#123;</span><br><span class="line">    filed1 := p1Type.Field(i)</span><br><span class="line">    fmt.Println(filed1.Tag)</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 方法2:</span></span><br><span class="line">filed2, _ := p1Type.FieldByName(<span class="string">&quot;name&quot;</span>)</span><br><span class="line">fmt.Println(filed2.Tag)</span><br><span class="line"><span class="comment">// 循环获取</span></span><br><span class="line">filedNum := p1Type.NumField()</span><br><span class="line"><span class="keyword">for</span> index := <span class="number">0</span>; index &lt; filedNum; index++ &#123;</span><br><span class="line">    field := p1Type.Field(index)</span><br><span class="line">    fmt.Println(field.Name, field.Tag)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="3-9-4-类型方法"><a href="#3-9-4-类型方法" class="headerlink" title="3.9.4 类型方法"></a>3.9.4 类型方法</h4><p>在开发过程中, 可以为type声明的类型编写一些方法, 从而实现对象.方法的操作</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> <span class="string">&quot;fmt&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 声明类型</span></span><br><span class="line"><span class="keyword">type</span> MyInt <span class="type">int</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 为MyInt类型定义一个指针方法</span></span><br><span class="line"><span class="comment">// 可以是指针/类型: *MyInt/MyInt</span></span><br><span class="line"><span class="comment">// 如果不使用对象, 可以使用 _ 代替</span></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="params">(i *MyInt)</span></span> add(n1 <span class="type">int</span>, n2 <span class="type">int</span>) <span class="type">int</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> n1 + n2 + <span class="type">int</span>(*i)</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">  <span class="keyword">var</span> v1 MyInt = <span class="number">1</span></span><br><span class="line">  res := v1.add(<span class="number">1</span>, <span class="number">2</span>)</span><br><span class="line">  fmt.Println(res)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>结构体也是基于type声明的类型, 所以也可以使用这种方式为结构体定义一些方法</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> <span class="string">&quot;fmt&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">type</span> Person <span class="keyword">struct</span> &#123;</span><br><span class="line">  name <span class="type">string</span></span><br><span class="line">  age  <span class="type">int</span></span><br><span class="line">  blog <span class="type">string</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 为Person结构体类型定义一个指针方法</span></span><br><span class="line"><span class="comment">// 注意: 此处如果不是指针类型的话, 再执行方法时, 结构体对象会被重新拷贝一份</span></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="params">(p *Person)</span></span> add(n1 <span class="type">int</span>, n2 <span class="type">int</span>) <span class="type">int</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> n1 + n2 + p.age</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">  p1 := Person&#123;name: <span class="string">&quot;李白&quot;</span>, age: <span class="number">18</span>, blog: <span class="string">&quot;123@qq.com&quot;</span>&#125;</span><br><span class="line">  res := p1.add(<span class="number">1</span>, <span class="number">2</span>)</span><br><span class="line">  fmt.Println(res)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>注意:</p>
<ul>
<li>在方法名之前, <code>func</code>关键字之后的括号中指定<code>receiver</code>. 如果方法不需要<code>recv</code>的值, 可以使用 _ 进行替换.</li>
<li><code>recv</code>类似于面向对象语言中的<code>self</code>或<code>this</code>, 但是Go中并没有这样的关键字.</li>
</ul>
<h4 id="3-9-5-方法继承"><a href="#3-9-5-方法继承" class="headerlink" title="3.9.5 方法继承"></a>3.9.5 方法继承</h4><p>如果结构体之间存在匿名继承关系, 则子结构体可以继承父结构体中的方法</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> <span class="string">&quot;fmt&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">type</span> Base <span class="keyword">struct</span> &#123;</span><br><span class="line">  name <span class="type">string</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">type</span> Son <span class="keyword">struct</span> &#123;</span><br><span class="line">  Base  <span class="comment">// 匿名方式, 如果改成base Base , 则无法继承Base的方法</span></span><br><span class="line">  age <span class="type">int</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// Base的结构体方法</span></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="params">(b *Base)</span></span> m1() <span class="type">int</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="number">666</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// Son的结构体方法</span></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="params">(s *Son)</span></span> m2() <span class="type">int</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="number">999</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">  son := Son(age: <span class="number">18</span>, Base: Base&#123;name: <span class="string">&quot;李白&quot;</span>&#125;)</span><br><span class="line">  res1 := son.m1()</span><br><span class="line">  res2 := son.m2()</span><br><span class="line">  fmt.Fprint(res1, res2)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>如果<code>Son</code>结构体中还有其他的结构体嵌套, 那么它可以继承所有嵌套结构体中的方法</p>
<h4 id="3-9-6-结构体工厂"><a href="#3-9-6-结构体工厂" class="headerlink" title="3.9.6 结构体工厂"></a>3.9.6 结构体工厂</h4><p>Go语言不支持面向对象编程中的构造方法, 但是可以在Go中实现”构造工厂”方法. 为了便捷, 通常会为类型定义一个工厂, 按照惯例, 以关键字<code>new</code>或<code>New</code>开头.</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> <span class="string">&quot;fmt&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">type</span> File <span class="keyword">struct</span> &#123;</span><br><span class="line">  fd   <span class="type">int</span></span><br><span class="line">  name <span class="type">string</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">NewFile</span><span class="params">(fd <span class="type">int</span>, name <span class="type">string</span>)</span></span> *File &#123;</span><br><span class="line">  <span class="keyword">return</span> &amp;File&#123;fd, name&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">  f := NewFile(<span class="number">10</span>, <span class="string">&quot;./test.txt&quot;</span>)</span><br><span class="line">  fmt.Println(f)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>强制使用工厂方法</strong>:</p>
<ul>
<li>让结构体变为私有, 工厂方法为共有</li>
<li>使用包管理 - 导包首字母大写公用的方式实现</li>
</ul>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> utils</span><br><span class="line"></span><br><span class="line"><span class="comment">// 私有结构体</span></span><br><span class="line"><span class="keyword">type</span> file <span class="keyword">struct</span> &#123;</span><br><span class="line">	fd <span class="type">int</span></span><br><span class="line">	name <span class="type">string</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 公有工厂方法</span></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">NewFile</span><span class="params">(fd <span class="type">int</span>, name <span class="type">string</span>)</span></span> *file &#123;</span><br><span class="line">  <span class="keyword">return</span> &amp;file&#123;fd, name&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">  <span class="string">&quot;fmt&quot;</span></span><br><span class="line">  <span class="string">&quot;utils&quot;</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">  obj := utils.NewFile(<span class="number">10</span>, <span class="string">&quot;xxx&quot;</span>)</span><br><span class="line">  fmt.Fprintf(obj)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h2 id="4-函数"><a href="#4-函数" class="headerlink" title="4 函数"></a>4 函数</h2><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> <span class="string">&quot;fmt&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 定义函数</span></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">SendEmail</span><span class="params">()</span></span> &#123;</span><br><span class="line">    fmt.Println(<span class="string">&quot;发送邮件&quot;</span>)</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">    <span class="comment">// 调用函数</span></span><br><span class="line">    SendEmail()</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="4-1-函数的定义"><a href="#4-1-函数的定义" class="headerlink" title="4.1 函数的定义"></a>4.1 函数的定义</h3><p>定义格式:</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> 函数名<span class="params">(参数)</span></span> 返回值 &#123;</span><br><span class="line">    函数体</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>示例:</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> <span class="string">&quot;fmt&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">SendEmail</span><span class="params">(email <span class="type">string</span>)</span></span> <span class="type">bool</span> &#123;</span><br><span class="line">    fmt.Println(email, <span class="string">&quot; 有新的邮件!&quot;</span>)</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">true</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">    res := SendEmail(<span class="string">&quot;123@qq.com&quot;</span>)</span><br><span class="line">    <span class="keyword">if</span> res &#123;</span><br><span class="line">        fmt.Println(<span class="string">&quot;success&quot;</span>)</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        fmt.Println(<span class="string">&quot;failed&quot;</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>关于函数名的注意事项:</p>
<blockquote>
<p>函数名只能是以字母, 数字, 下划线组成, 并且不能以数字开头. (建议遵循驼峰命名规范)</p>
</blockquote>
<h3 id="4-2-函数的参数"><a href="#4-2-函数的参数" class="headerlink" title="4.2 函数的参数"></a>4.2 函数的参数</h3><h4 id="4-2-1-多个参数"><a href="#4-2-1-多个参数" class="headerlink" title="4.2.1 多个参数"></a>4.2.1 多个参数</h4><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> <span class="string">&quot;fmt&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">add</span><span class="params">(num1 <span class="type">int</span>, num2 <span class="type">int</span>)</span></span> (<span class="type">int</span>, <span class="type">bool</span>) &#123;</span><br><span class="line">  res := num1 + num2</span><br><span class="line">  flag := <span class="literal">true</span></span><br><span class="line">  <span class="keyword">return</span> res, flag</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">  data, flag := add(<span class="number">1</span>, <span class="number">9</span>)</span><br><span class="line">  fmt.Println(data, flag)</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 10 true</span></span><br></pre></td></tr></table></figure>

<p>在传值时会拷贝一份数据(等同于赋值拷贝)</p>
<h4 id="4-2-2-指针参数"><a href="#4-2-2-指针参数" class="headerlink" title="4.2.2 指针参数"></a>4.2.2 指针参数</h4><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> <span class="string">&quot;fmt&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">changeData</span><span class="params">(data *[2]<span class="type">int</span>)</span></span> <span class="type">bool</span> &#123;</span><br><span class="line">  data[<span class="number">1</span>] = <span class="number">123</span></span><br><span class="line">  <span class="keyword">return</span> <span class="literal">true</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">  data := [<span class="number">2</span>]<span class="type">int</span>&#123;<span class="number">12</span>, <span class="number">34</span>&#125;</span><br><span class="line">  _ = changeData(&amp;data)</span><br><span class="line">  fmt.Println(data)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="4-2-3-函数做参数"><a href="#4-2-3-函数做参数" class="headerlink" title="4.2.3 函数做参数"></a>4.2.3 函数做参数</h4><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> <span class="string">&quot;fmt&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">add100</span><span class="params">(arg <span class="type">int</span>)</span></span> (<span class="type">int</span>, <span class="type">bool</span>) &#123;</span><br><span class="line">  <span class="keyword">return</span> arg + <span class="number">100</span>, <span class="literal">true</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 别名</span></span><br><span class="line"><span class="comment">// type f1 func(int) (int, bool)</span></span><br><span class="line"><span class="comment">// func proxy(data int, exec f1) int</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">proxy</span><span class="params">(data <span class="type">int</span>, exec <span class="keyword">func</span>(<span class="type">int</span>)</span></span> (<span class="type">int</span>, <span class="type">bool</span>)) <span class="type">int</span> &#123;</span><br><span class="line">  data, flag := exec(data)</span><br><span class="line">  <span class="keyword">if</span> flag &#123;</span><br><span class="line">    <span class="keyword">return</span> data</span><br><span class="line">  &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">9999</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">  res := proxy(<span class="number">123</span>, add100)</span><br><span class="line">  fmt.Println(res) <span class="comment">// 223</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="4-2-4-变长参数"><a href="#4-2-4-变长参数" class="headerlink" title="4.2.4 变长参数"></a>4.2.4 变长参数</h4><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> <span class="string">&quot;fmt&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 任意数量个参数 - 切片类型</span></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">do</span><span class="params">(num ...<span class="type">int</span>)</span></span> <span class="type">int</span> &#123;</span><br><span class="line">  sum := <span class="number">0</span></span><br><span class="line">  <span class="keyword">for</span> _, val := <span class="keyword">range</span> num &#123;</span><br><span class="line">    sum += val</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> sum</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">  res := do(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>)</span><br><span class="line">  fmt.Println(res) <span class="comment">// 10</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>注意事项:</p>
<ul>
<li>变长参数只能有一个</li>
<li>如果有多个参数, 变长参数需要放到最后</li>
</ul>
<h3 id="4-3-匿名函数"><a href="#4-3-匿名函数" class="headerlink" title="4.3 匿名函数"></a>4.3 匿名函数</h3><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> <span class="string">&quot;fmt&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">  <span class="comment">// 匿名函数</span></span><br><span class="line">  v1 := <span class="function"><span class="keyword">func</span><span class="params">(n1 <span class="type">int</span>)</span></span> <span class="type">int</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> n1 + <span class="number">100</span></span><br><span class="line">  &#125;</span><br><span class="line">  data := v1(<span class="number">123</span>)</span><br><span class="line">  fmt.Println(data)</span><br><span class="line">  v2 := <span class="function"><span class="keyword">func</span><span class="params">(n1 <span class="type">int</span>)</span></span> <span class="type">int</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> n1 + <span class="number">123</span></span><br><span class="line">  &#125;(<span class="number">12</span>)</span><br><span class="line">  fmt.Println(v2)</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">F1</span><span class="params">(n1 <span class="type">int</span>, n2 <span class="type">int</span>)</span></span> <span class="function"><span class="keyword">func</span><span class="params">(<span class="type">int</span>)</span></span> <span class="type">string</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="function"><span class="keyword">func</span><span class="params">(n1 <span class="type">int</span>)</span></span> <span class="type">string</span> &#123;</span><br><span class="line">    fmt.Println(<span class="string">&quot;匿名&quot;</span>)</span><br><span class="line">      <span class="keyword">return</span> <span class="string">&quot;匿名&quot;</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="4-4-闭包"><a href="#4-4-闭包" class="headerlink" title="4.4 闭包"></a>4.4 闭包</h3><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 存储5个函数的切片</span></span><br><span class="line"><span class="keyword">var</span> funList []<span class="function"><span class="keyword">func</span><span class="params">()</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> i := <span class="number">0</span>; i &lt; <span class="number">5</span>; i++ &#123;</span><br><span class="line">  fun := <span class="function"><span class="keyword">func</span><span class="params">()</span></span> &#123;</span><br><span class="line">    fmt.Println(<span class="string">&quot;i =&quot;</span>, i)</span><br><span class="line">  &#125;</span><br><span class="line">  funList = <span class="built_in">append</span>(funList, fun)</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 运行函数</span></span><br><span class="line"><span class="keyword">for</span> i := <span class="number">0</span>; i &lt; <span class="number">5</span>; i++ &#123;</span><br><span class="line">  funList[i]()</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// i = 5</span></span><br><span class="line"><span class="comment">// i = 5</span></span><br><span class="line"><span class="comment">// i = 5</span></span><br><span class="line"><span class="comment">// i = 5</span></span><br><span class="line"><span class="comment">// i = 5</span></span><br></pre></td></tr></table></figure>

<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 存储5个函数的切片</span></span><br><span class="line"><span class="keyword">var</span> funList []<span class="function"><span class="keyword">func</span><span class="params">()</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> i := <span class="number">0</span>; i &lt; <span class="number">5</span>; i++ &#123;</span><br><span class="line">  fun := <span class="function"><span class="keyword">func</span><span class="params">(arg <span class="type">int</span>)</span></span> <span class="function"><span class="keyword">func</span><span class="params">()</span></span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="function"><span class="keyword">func</span><span class="params">()</span></span> &#123;</span><br><span class="line">      fmt.Println(<span class="string">&quot;i =&quot;</span>, arg)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;(i)</span><br><span class="line">  funList = <span class="built_in">append</span>(funList, fun)</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 运行函数</span></span><br><span class="line"><span class="keyword">for</span> i := <span class="number">0</span>; i &lt; <span class="number">5</span>; i++ &#123;</span><br><span class="line">  funList[i]()</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// i = 0</span></span><br><span class="line"><span class="comment">// i = 1</span></span><br><span class="line"><span class="comment">// i = 2</span></span><br><span class="line"><span class="comment">// i = 3</span></span><br><span class="line"><span class="comment">// i = 4</span></span><br></pre></td></tr></table></figure>

<h3 id="4-5-defer"><a href="#4-5-defer" class="headerlink" title="4.5 defer"></a>4.5 defer</h3><p>用于在一个函数执行完之后自动触发的语句, 一般用于结束操作之后释放资源, 如果有多个defer, 按照倒序执行. 如果在定义defer前已经执行了return语句, 则不会执行defer语句.</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> <span class="string">&quot;fmt&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">do</span><span class="params">()</span></span> <span class="type">int</span> &#123;</span><br><span class="line">  fmt.Println(<span class="string">&quot;hello&quot;</span>)</span><br><span class="line">  <span class="keyword">defer</span> fmt.Println(<span class="string">&quot;123&quot;</span>)</span><br><span class="line">  <span class="keyword">defer</span> fmt.Println(<span class="string">&quot;456&quot;</span>)</span><br><span class="line">  fmt.Println(<span class="string">&quot;world&quot;</span>)</span><br><span class="line">  <span class="keyword">return</span> <span class="number">1234</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">  res := do()</span><br><span class="line">  fmt.Println(res)</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// hello</span></span><br><span class="line"><span class="comment">// world</span></span><br><span class="line"><span class="comment">// 456</span></span><br><span class="line"><span class="comment">// 123</span></span><br><span class="line"><span class="comment">// 1234</span></span><br></pre></td></tr></table></figure>

</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://crappier.github.io">fanhao</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://crappier.github.io/2023/05/16/Go%E8%AF%AD%E8%A8%80%E5%AD%A6%E4%B9%A0/">https://crappier.github.io/2023/05/16/Go%E8%AF%AD%E8%A8%80%E5%AD%A6%E4%B9%A0/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://crappier.github.io" target="_blank">Depicter</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Go%E8%AF%AD%E8%A8%80/">Go语言</a></div><div class="post_share"><div class="social-share" data-image="/img/cover/cover_4.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2023/05/20/Django%E5%BC%80%E5%8F%91%E7%9F%A5%E8%AF%86%E7%82%B9/" title="Django开发知识点"><img class="cover" src="/img/cover/cover_6.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">Django开发知识点</div></div></a></div><div class="next-post pull-right"><a href="/2023/04/13/%E5%A4%A7%E6%95%B0%E6%8D%AE%E5%85%A5%E9%97%A8/" title="大数据入门"><img class="cover" src="/img/cover/cover_4.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">大数据入门</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/avatar.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">fanhao</div><div class="author-info__description"></div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">10</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">5</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">0</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://gitee.com/fanhao0416"><i class="fab fa-gitee"></i><span>联系我</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://gitee.com/fanhao0416" target="_blank" title="Gitee"><i class="fab fa-git" style="color: #DC143C;"></i></a><a class="social-icon" href="mailto:2039216364@qq.com" target="_blank" title="Email"><i class="fas fa-envelope" style="color: #4a7dbe;"></i></a><a class="social-icon" href="http://wpa.qq.com/msgrd?v=3&amp;uin=2039216364&amp;site=qq&amp;menu=yes" target="_blank" title="QQ"><i class="fab fa-qq" style="color: #4a7dbe;"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">沧海易幻莫同，世人皆平庸</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#Go%E8%AF%AD%E8%A8%80%E5%AD%A6%E4%B9%A0"><span class="toc-number">1.</span> <span class="toc-text">Go语言学习</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-%E5%BF%AB%E9%80%9F%E4%B8%8A%E6%89%8B"><span class="toc-number">1.1.</span> <span class="toc-text">1 快速上手</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-1-%E5%8C%85%E7%AE%A1%E7%90%86"><span class="toc-number">1.1.1.</span> <span class="toc-text">1.1 包管理</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-2-%E8%BE%93%E5%87%BA"><span class="toc-number">1.1.2.</span> <span class="toc-text">1.2 输出</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-3-%E6%B3%A8%E9%87%8A"><span class="toc-number">1.1.3.</span> <span class="toc-text">1.3 注释</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-4-%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B-%E5%88%9D%E5%A7%8B"><span class="toc-number">1.1.4.</span> <span class="toc-text">1.4 数据类型(初始)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-5-%E5%8F%98%E9%87%8F"><span class="toc-number">1.1.5.</span> <span class="toc-text">1.5 变量</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-5-1-%E5%8F%98%E9%87%8F%E5%90%8D%E7%9A%84%E8%A6%81%E6%B1%82"><span class="toc-number">1.1.5.1.</span> <span class="toc-text">1.5.1 变量名的要求</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1-5-2-%E5%8F%98%E9%87%8F%E7%AE%80%E5%86%99"><span class="toc-number">1.1.5.2.</span> <span class="toc-text">1.5.2 变量简写</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1-5-3-%E5%8F%98%E9%87%8F%E4%BD%9C%E7%94%A8%E5%9F%9F"><span class="toc-number">1.1.5.3.</span> <span class="toc-text">1.5.3 变量作用域</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1-5-4-%E8%B5%8B%E5%80%BC%E5%8F%8A%E5%86%85%E5%AD%98%E7%9B%B8%E5%85%B3"><span class="toc-number">1.1.5.4.</span> <span class="toc-text">1.5.4 赋值及内存相关</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-6-%E5%B8%B8%E9%87%8F"><span class="toc-number">1.1.6.</span> <span class="toc-text">1.6 常量</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%AE%9A%E4%B9%89"><span class="toc-number">1.1.6.1.</span> <span class="toc-text">定义</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#iota"><span class="toc-number">1.1.6.2.</span> <span class="toc-text">iota</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-7-%E8%BE%93%E5%85%A5"><span class="toc-number">1.1.7.</span> <span class="toc-text">1.7 输入</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-8-%E6%9D%A1%E4%BB%B6%E8%AF%AD%E5%8F%A5"><span class="toc-number">1.1.8.</span> <span class="toc-text">1.8 条件语句</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86"><span class="toc-number">1.2.</span> <span class="toc-text">2 基础知识</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#2-1-switch%E8%AF%AD%E5%8F%A5"><span class="toc-number">1.2.1.</span> <span class="toc-text">2.1 switch语句</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-2-for%E5%BE%AA%E7%8E%AF"><span class="toc-number">1.2.2.</span> <span class="toc-text">2.2 for循环</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-3-for%E5%BE%AA%E7%8E%AF%E6%89%93%E6%A0%87%E7%AD%BE"><span class="toc-number">1.2.3.</span> <span class="toc-text">2.3 for循环打标签</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-4-goto%E8%AF%AD%E5%8F%A5"><span class="toc-number">1.2.4.</span> <span class="toc-text">2.4 goto语句</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-5-%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%A0%BC%E5%BC%8F%E5%8C%96"><span class="toc-number">1.2.5.</span> <span class="toc-text">2.5 字符串格式化</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-6-%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-number">1.2.6.</span> <span class="toc-text">2.6 运算符</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B"><span class="toc-number">1.3.</span> <span class="toc-text">3 数据类型</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#3-1-%E6%95%B4%E5%9E%8B"><span class="toc-number">1.3.1.</span> <span class="toc-text">3.1 整型</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#3-1-1-%E7%B1%BB%E5%9E%8B"><span class="toc-number">1.3.1.1.</span> <span class="toc-text">3.1.1 类型</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-1-2-%E6%95%B4%E5%9E%8B%E4%B9%8B%E9%97%B4%E7%9B%B8%E4%BA%92%E8%BD%AC%E6%8D%A2"><span class="toc-number">1.3.1.2.</span> <span class="toc-text">3.1.2 整型之间相互转换</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-1-3-%E6%95%B4%E5%9E%8B%E4%B8%8E%E5%AD%97%E7%AC%A6%E4%B8%B2%E8%BD%AC%E6%8D%A2"><span class="toc-number">1.3.1.3.</span> <span class="toc-text">3.1.3 整型与字符串转换</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-1-4-%E8%BF%9B%E5%88%B6%E8%BD%AC%E6%8D%A2"><span class="toc-number">1.3.1.4.</span> <span class="toc-text">3.1.4 进制转换</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-1-5-%E5%B8%B8%E8%A7%81%E6%95%B0%E5%AD%A6%E8%BF%90%E7%AE%97"><span class="toc-number">1.3.1.5.</span> <span class="toc-text">3.1.5 常见数学运算</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-1-6-%E6%8C%87%E9%92%88-x2F-nil-x2F-%E5%A3%B0%E6%98%8E%E5%8F%98%E9%87%8F-x2F-new"><span class="toc-number">1.3.1.6.</span> <span class="toc-text">3.1.6 指针&#x2F;nil&#x2F;声明变量&#x2F;new</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-1-7-%E8%B6%85%E5%A4%A7%E6%95%B4%E5%9E%8B"><span class="toc-number">1.3.1.7.</span> <span class="toc-text">3.1.7 超大整型</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-2-%E6%B5%AE%E7%82%B9%E5%9E%8B"><span class="toc-number">1.3.2.</span> <span class="toc-text">3.2 浮点型</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#3-2-1-%E7%B1%BB%E5%9E%8B"><span class="toc-number">1.3.2.1.</span> <span class="toc-text">3.2.1 类型</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-2-2-%E9%9D%9E%E7%B2%BE%E7%A1%AE"><span class="toc-number">1.3.2.2.</span> <span class="toc-text">3.2.2 非精确</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-2-3-deciaml"><span class="toc-number">1.3.2.3.</span> <span class="toc-text">3.2.3 deciaml</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-3-%E5%B8%83%E5%B0%94%E7%B1%BB%E5%9E%8B"><span class="toc-number">1.3.3.</span> <span class="toc-text">3.3 布尔类型</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-4-%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%B1%BB%E5%9E%8B"><span class="toc-number">1.3.4.</span> <span class="toc-text">3.4 字符串类型</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#3-4-1-%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9A%84%E6%9C%AC%E8%B4%A8"><span class="toc-number">1.3.4.1.</span> <span class="toc-text">3.4.1 字符串的本质</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-4-2-%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9A%84%E5%B8%B8%E8%A7%81%E5%8A%9F%E8%83%BD"><span class="toc-number">1.3.4.2.</span> <span class="toc-text">3.4.2 字符串的常见功能</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-4-3-%E7%B4%A2%E5%BC%95%E5%88%87%E7%89%87%E5%92%8C%E5%BE%AA%E7%8E%AF"><span class="toc-number">1.3.4.3.</span> <span class="toc-text">3.4.3 索引切片和循环</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-5-%E6%95%B0%E7%BB%84"><span class="toc-number">1.3.5.</span> <span class="toc-text">3.5 数组</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#3-5-1-%E6%95%B0%E7%BB%84%E5%A3%B0%E6%98%8E"><span class="toc-number">1.3.5.1.</span> <span class="toc-text">3.5.1 数组声明</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-5-2-%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86"><span class="toc-number">1.3.5.2.</span> <span class="toc-text">3.5.2 内存管理</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-5-3-%E5%8F%AF%E5%8F%98%E5%92%8C%E6%8B%B7%E8%B4%9D"><span class="toc-number">1.3.5.3.</span> <span class="toc-text">3.5.3 可变和拷贝</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-5-4-%E9%95%BF%E5%BA%A6%E7%B4%A2%E5%BC%95%E5%88%87%E7%89%87%E4%B8%8E%E5%BE%AA%E7%8E%AF"><span class="toc-number">1.3.5.4.</span> <span class="toc-text">3.5.4 长度索引切片与循环</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-5-5-%E6%95%B0%E7%BB%84%E5%B5%8C%E5%A5%97"><span class="toc-number">1.3.5.5.</span> <span class="toc-text">3.5.5 数组嵌套</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-6-%E5%88%87%E7%89%87"><span class="toc-number">1.3.6.</span> <span class="toc-text">3.6 切片</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#3-6-1-%E5%88%87%E7%89%87%E7%9A%84%E5%AD%98%E5%82%A8"><span class="toc-number">1.3.6.1.</span> <span class="toc-text">3.6.1 切片的存储</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-6-2-%E5%88%9B%E5%BB%BA%E5%88%87%E7%89%87"><span class="toc-number">1.3.6.2.</span> <span class="toc-text">3.6.2 创建切片</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-6-3-%E5%88%87%E7%89%87%E7%9A%84%E8%87%AA%E5%8A%A8%E6%89%A9%E5%AE%B9"><span class="toc-number">1.3.6.3.</span> <span class="toc-text">3.6.3 切片的自动扩容</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-6-4-%E5%8F%98%E9%87%8F%E8%B5%8B%E5%80%BC"><span class="toc-number">1.3.6.4.</span> <span class="toc-text">3.6.4 变量赋值</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-7-%E5%AD%97%E5%85%B8%E7%B1%BB%E5%9E%8B-Map"><span class="toc-number">1.3.7.</span> <span class="toc-text">3.7 字典类型(Map)</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#3-7-1-%E5%A3%B0%E6%98%8E-amp-%E5%88%9D%E5%A7%8B%E5%8C%96"><span class="toc-number">1.3.7.1.</span> <span class="toc-text">3.7.1 声明&amp;初始化</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-7-2-map%E7%9A%84%E5%B8%B8%E7%94%A8%E6%93%8D%E4%BD%9C"><span class="toc-number">1.3.7.2.</span> <span class="toc-text">3.7.2 map的常用操作</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-7-3-map%E5%BA%95%E5%B1%82%E5%8E%9F%E7%90%86%E5%89%96%E6%9E%90"><span class="toc-number">1.3.7.3.</span> <span class="toc-text">3.7.3 map底层原理剖析</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#3-7-3-1-%E5%88%9D%E5%A7%8B%E5%8C%96"><span class="toc-number">1.3.7.3.1.</span> <span class="toc-text">3.7.3.1 初始化</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#3-7-3-2-%E5%86%99%E5%85%A5%E6%95%B0%E6%8D%AE"><span class="toc-number">1.3.7.3.2.</span> <span class="toc-text">3.7.3.2 写入数据</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#3-7-3-3-%E8%AF%BB%E5%8F%96%E6%95%B0%E6%8D%AE"><span class="toc-number">1.3.7.3.3.</span> <span class="toc-text">3.7.3.3 读取数据</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#3-7-3-4-%E6%89%A9%E5%AE%B9"><span class="toc-number">1.3.7.3.4.</span> <span class="toc-text">3.7.3.4 扩容</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#3-7-3-5-%E8%BF%81%E7%A7%BB"><span class="toc-number">1.3.7.3.5.</span> <span class="toc-text">3.7.3.5 迁移</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-8-%E6%8C%87%E9%92%88"><span class="toc-number">1.3.8.</span> <span class="toc-text">3.8 指针</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#3-8-1-%E6%8C%87%E9%92%88%E7%9A%84%E5%AE%9A%E4%B9%89"><span class="toc-number">1.3.8.1.</span> <span class="toc-text">3.8.1 指针的定义</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-8-2-%E6%8C%87%E9%92%88%E7%9A%84%E5%BA%94%E7%94%A8%E5%9C%BA%E6%99%AF"><span class="toc-number">1.3.8.2.</span> <span class="toc-text">3.8.2 指针的应用场景</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-8-3-%E8%A1%A5%E5%85%85"><span class="toc-number">1.3.8.3.</span> <span class="toc-text">3.8.3 补充</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-9-%E7%BB%93%E6%9E%84%E4%BD%93"><span class="toc-number">1.3.9.</span> <span class="toc-text">3.9 结构体</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#3-9-1-%E7%BB%93%E6%9E%84%E4%BD%93%E5%AE%9A%E4%B9%89"><span class="toc-number">1.3.9.1.</span> <span class="toc-text">3.9.1 结构体定义</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-9-2-%E5%88%9D%E5%A7%8B%E5%8C%96"><span class="toc-number">1.3.9.2.</span> <span class="toc-text">3.9.2 初始化</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-9-3-%E7%BB%93%E6%9E%84%E4%BD%93%E6%A0%87%E7%AD%BE"><span class="toc-number">1.3.9.3.</span> <span class="toc-text">3.9.3 结构体标签</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-9-4-%E7%B1%BB%E5%9E%8B%E6%96%B9%E6%B3%95"><span class="toc-number">1.3.9.4.</span> <span class="toc-text">3.9.4 类型方法</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-9-5-%E6%96%B9%E6%B3%95%E7%BB%A7%E6%89%BF"><span class="toc-number">1.3.9.5.</span> <span class="toc-text">3.9.5 方法继承</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-9-6-%E7%BB%93%E6%9E%84%E4%BD%93%E5%B7%A5%E5%8E%82"><span class="toc-number">1.3.9.6.</span> <span class="toc-text">3.9.6 结构体工厂</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-%E5%87%BD%E6%95%B0"><span class="toc-number">1.4.</span> <span class="toc-text">4 函数</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#4-1-%E5%87%BD%E6%95%B0%E7%9A%84%E5%AE%9A%E4%B9%89"><span class="toc-number">1.4.1.</span> <span class="toc-text">4.1 函数的定义</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-2-%E5%87%BD%E6%95%B0%E7%9A%84%E5%8F%82%E6%95%B0"><span class="toc-number">1.4.2.</span> <span class="toc-text">4.2 函数的参数</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#4-2-1-%E5%A4%9A%E4%B8%AA%E5%8F%82%E6%95%B0"><span class="toc-number">1.4.2.1.</span> <span class="toc-text">4.2.1 多个参数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-2-2-%E6%8C%87%E9%92%88%E5%8F%82%E6%95%B0"><span class="toc-number">1.4.2.2.</span> <span class="toc-text">4.2.2 指针参数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-2-3-%E5%87%BD%E6%95%B0%E5%81%9A%E5%8F%82%E6%95%B0"><span class="toc-number">1.4.2.3.</span> <span class="toc-text">4.2.3 函数做参数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-2-4-%E5%8F%98%E9%95%BF%E5%8F%82%E6%95%B0"><span class="toc-number">1.4.2.4.</span> <span class="toc-text">4.2.4 变长参数</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-3-%E5%8C%BF%E5%90%8D%E5%87%BD%E6%95%B0"><span class="toc-number">1.4.3.</span> <span class="toc-text">4.3 匿名函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-4-%E9%97%AD%E5%8C%85"><span class="toc-number">1.4.4.</span> <span class="toc-text">4.4 闭包</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-5-defer"><span class="toc-number">1.4.5.</span> <span class="toc-text">4.5 defer</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2024/03/12/Nmap%E4%BD%BF%E7%94%A8%E6%8C%87%E5%8D%97/" title="Nmap使用指南"><img src="/img/cover/cover_2.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Nmap使用指南"/></a><div class="content"><a class="title" href="/2024/03/12/Nmap%E4%BD%BF%E7%94%A8%E6%8C%87%E5%8D%97/" title="Nmap使用指南">Nmap使用指南</a><time datetime="2024-03-12T02:38:29.000Z" title="发表于 2024-03-12 10:38:29">2024-03-12</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2024/03/10/TCP%E5%8D%8F%E8%AE%AE/" title="TCP协议"><img src="/img/cover/cover_9.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="TCP协议"/></a><div class="content"><a class="title" href="/2024/03/10/TCP%E5%8D%8F%E8%AE%AE/" title="TCP协议">TCP协议</a><time datetime="2024-03-10T08:15:52.000Z" title="发表于 2024-03-10 16:15:52">2024-03-10</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2024/03/06/%E5%B8%B8%E7%94%A8%E7%BD%91%E7%BB%9C%E5%AE%89%E5%85%A8%E8%AE%BE%E5%A4%87/" title="常用网络安全设备"><img src="/img/cover/cover_6.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="常用网络安全设备"/></a><div class="content"><a class="title" href="/2024/03/06/%E5%B8%B8%E7%94%A8%E7%BD%91%E7%BB%9C%E5%AE%89%E5%85%A8%E8%AE%BE%E5%A4%87/" title="常用网络安全设备">常用网络安全设备</a><time datetime="2024-03-06T00:58:05.000Z" title="发表于 2024-03-06 08:58:05">2024-03-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2024/03/06/%E6%B8%97%E9%80%8F%E6%B5%8B%E8%AF%95/" title="渗透测试"><img src="/img/cover/cover_9.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="渗透测试"/></a><div class="content"><a class="title" href="/2024/03/06/%E6%B8%97%E9%80%8F%E6%B5%8B%E8%AF%95/" title="渗透测试">渗透测试</a><time datetime="2024-03-06T00:57:01.000Z" title="发表于 2024-03-06 08:57:01">2024-03-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2024/03/06/OWASP-TOP-10/" title="OWASP-TOP-10"><img src="/img/cover/cover_2.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="OWASP-TOP-10"/></a><div class="content"><a class="title" href="/2024/03/06/OWASP-TOP-10/" title="OWASP-TOP-10">OWASP-TOP-10</a><time datetime="2024-03-06T00:55:41.000Z" title="发表于 2024-03-06 08:55:41">2024-03-06</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2023 - 2025 By fanhao</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.umd.min.js"></script><div class="js-pjax"></div><script defer="defer" id="ribbon" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/canvas-ribbon.min.js" size="150" alpha="0.6" zIndex="-1" mobile="true" data-click="false"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>